Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ clang不编译我的代码,但是g++;做_C++_Templates_C++11 - Fatal编程技术网

C++ clang不编译我的代码,但是g++;做

C++ clang不编译我的代码,但是g++;做,c++,templates,c++11,C++,Templates,C++11,有人能帮我写这个代码吗 #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; }

有人能帮我写这个代码吗

#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;
}
#包括
#包括
结构无义{};
模板
typename std::enable_if::value,int>::type
fo(无效*常数)
{
返回0;
}
模板
typename std::enable_if::type
fo(无效*常数)
{
返回1;
}
typedef int(*func_type)(void*);
模板
void run_me()
{
静态结构无意义数据;
typedef std::成对类型;
std::向量v;
v、 推回(成对{“a”,fo});
v、 推回(成对{“b”,fo});
}
int main(int,char*[])
{
快跑;
返回0;
}
clang-3.3不编译此代码,但g++-4.8.1编译此代码,这两个编译器中哪一个是正确的?我怀疑代码有问题吗

错误内容如下:

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> });
              ^        ~~~~~~~~~~~~~~~~~~~~~~~~
a.cpp:32:15:错误:没有用于初始化“pair_type”(又名“pair”)的匹配构造函数
v、 推回(成对{“a”,fo});
^        ~~~~~~~~~~~~~~~~~~~~~~~
a、 cpp:33:15:错误:没有用于初始化“pair_type”(也称为“pair”)的匹配构造函数
v、 推回(成对{“b”,fo});
^        ~~~~~~~~~~~~~~~~~~~~~~~~

静态结构无意义数据
重新定位到函数外部可获得要编译的代码。我没有足够的头脑告诉你为什么

要为
O
参数的不同值定制
数据
,可以定义
无意义
,如下所示

template <size_t> struct nonsense {
    static nonsense 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, 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>);
}
模板
typename std::enable_if::value,int>::type
fo(无效*常数)
{
//使用无意义::数据
}
模板
typename std::enable_if::type
fo(无效*常数)
{
//使用无意义::数据
}
⋮
模板
void run_me()
{
std::向量v;
v、 向后安放(“a”,fo);
v、 向后安放(“b”,fo);
}

是否通过-std=C++11启用了C++11?是的,我启用了,gnu++11
func_type f1=fo也会失败。icc 13.0.1也会编译我会剪切你的代码。它仍然使用g++,而使用clangg++失败。另外,如果您取消注释line
auto f2=foo-g++也将失败。是的,但我需要它在函数模板体中。公平点,但现在我想知道为什么需要这样做。如果不了解
ptr
模板参数的用途,很难理解为什么必须针对静态结构而不是模板参数
O
定义
fo
,它提供了相同程度的唯一性。可以在函数模板实例化中修改带有成员的非空结构,然后可以使用指向此修改结构的指针实例化函数模板。例如,可以根据
O
模板参数设置成员。为了方便起见,我使用了静态
struct
来绕过
char const*
作为模板参数的问题。
struct
包含一个
char const*
成员,否则我无法将其用于模板实例化。@user1095108:我已相应地修改了答案。我希望我正确地理解了你。