c – clang不编译我的代码,但g确实如此
发布时间:2020-12-16 07:04:56 所属栏目:百科 来源:网络整理
导读:有人可以帮我这个代码: #include type_traits#include vectorstruct nonsense { };template struct nonsense const* ptr,typename Rtypename std::enable_if!std::is_voidR::value,int::typefo(void* const){ return 0;}template struct nonsense const* pt
有人可以帮我这个代码:
#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>); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |