C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – clang不编译我的代码,但g确实如此大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
有人可以帮我这个代码

#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",void> });
}

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

  return 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",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",void> });
              ^        ~~~~~~~~~~~~~~~~~~~~~~~~

解决方法

函数外部重新定位静态struct nonsense数据会获得要编译的代码.我不够精明,不能告诉你原因.

要为O参数的不同值自定义数据,可以按如下方式定义废话…

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

……并因此使用它……

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

template <std::size_t O,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",void>);
}

大佬总结

以上是大佬教程为你收集整理的c – clang不编译我的代码,但g确实如此全部内容,希望文章能够帮你解决c – clang不编译我的代码,但g确实如此所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。