4

Can someone help me with this code:

#include <type_traits>

#include <vector>

struct nonsense { };

template <struct nonsense const* ptr, typename R>
typename std::enable_if<!std::is_void<R>::value, int>::type
fo(void* const)
{
  return 0;
}

template <struct nonsense const* ptr, typename R>
typename std::enable_if<std::is_void<R>::value, int>::type
fo(void* const)
{
  return 1;
}

typedef int (*func_type)(void*);

template <std::size_t O>
void run_me()
{
  static struct nonsense data;

  typedef std::pair<char const* const, func_type> pair_type;

  std::vector<pair_type> v;

  v.push_back(pair_type{ "a", fo<&data, int> });
  v.push_back(pair_type{ "b", fo<&data, void> });
}

int main(int, char*[])
{
  run_me<2>();

  return 0;
}

clang-3.3 does not compile this code, but g++-4.8.1 does, which of the two compiler is right? Is something wrong with the code, as I suspect?

The error reads:

a.cpp:32:15: error: no matching constructor for initialization of 'pair_type' (aka 'pair<const char *const, func_type>')
  v.push_back(pair_type{ "a", fo<&data, int> });
              ^        ~~~~~~~~~~~~~~~~~~~~~~~
a.cpp:33:15: error: no matching constructor for initialization of 'pair_type' (aka 'pair<const char *const, func_type>')
  v.push_back(pair_type{ "b", fo<&data, void> });
              ^        ~~~~~~~~~~~~~~~~~~~~~~~~
8
  • did you enable C++11 by -std=c++11 ? Commented Jun 27, 2013 at 10:05
  • yes, I did and also gnu++11 Commented Jun 27, 2013 at 10:05
  • func_type f1 = fo<&data, int>; fails too. Commented Jun 27, 2013 at 10:12
  • icc 13.0.1 also compiles Commented Jun 27, 2013 at 10:30
  • I cut your code. It still compiles with g++ and fails with clang++. Also, if you uncomment line auto f2 = foo<&data>; - g++ will fails too. Commented Jun 27, 2013 at 10:51

1 Answer 1

1

Relocating static struct nonsense data outside the function gets the code to compile. I'm not savvy enough to tell you why.

To customise data for different values of the O parameter, one could define nonsense as follows…

template <size_t> struct nonsense {
    static nonsense data;
    ⋮
};

…and use it thus…

template <std::size_t O, typename R>
typename std::enable_if<!std::is_void<R>::value, int>::type
fo(void* const)
{
  // Use nonsense<O>::data
}

template <std::size_t O, typename R>
typename std::enable_if<std::is_void<R>::value, int>::type
fo(void* const)
{
  // Use nonsense<O>::data
}

⋮

template <std::size_t O>
void run_me()
{
  std::vector<std::pair<char const* const, func_type>> v;

  v.emplace_back("a", fo<O, int >);
  v.emplace_back("b", fo<O, void>);
}
Sign up to request clarification or add additional context in comments.

8 Comments

Yes, but I need it in the function template body.
Fair point, but now I'm wondering why you need to do this. Without understanding the purpose of the ptr template parameter, it's difficult to see why you have to define fo with respect to a static struct instead of the template parameter O, which provides the same degree of uniqueness.
a non-empty struct with members can be modified in run_me function template instantiations and a pointer to this modified struct can then be used to instantiate an fo function template. Say, a member could be set depending on the O template parameter. It's a matter of convenience.
I was using the static struct to circumvent the char const* as template parameter problem. The struct contained a char const* member, that I otherwise could not use for template instantiation.
@user1095108: I've amended my answer accordingly. I hope I've understood you correctly.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.