clang不编译我的代码,但是g ++ [英] clang does not compile my code, but g++ does

查看:146
本文介绍了clang不编译我的代码,但是g ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我使用此代码:

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不编译此代码,但g ++ - 4.8.1 ,哪两个编译器是对的?

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?

错误讯息如下:

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> });
              ^        ~~~~~~~~~~~~~~~~~~~~~~~~


推荐答案

重定位 static struct nonsense data

要自定义数据,以获取<$ c的不同值$ c> O 参数,可以定义无义如下:

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>);
}

这篇关于clang不编译我的代码,但是g ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆