[英]clang does not compile my code, but g++ does
有人可以幫我這個代碼:
#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呢,兩個編譯器中的哪一個是對的? 我懷疑代碼有問題嗎?
錯誤如下:
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
會獲得要編譯的代碼。 我不夠精明,不能告訴你原因。
要為O
參數的不同值自定義data
,可以按如下方式定義nonsense
...
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>);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.