【发布时间】:2012-08-18 19:56:36
【问题描述】:
我想我已经盯着这个太久了,但我在这里找不到我的错误:
struct
{
bool empty() const
{
return true;
}
} hasEmpty;
template<typename T>
struct has_empty
{
private:
template<typename U, U>
class check {};
template<typename C>
static char f(check<void (C::*)() const, &C::empty> *);
template<typename C>
static long f(...);
public:
static const bool value = (sizeof(f<T>(nullptr)) == sizeof(char));
};
template<typename T>
typename std::enable_if<has_empty<T>::value>::type foo(const T& t)
{
}
void x()
{
foo(hasEmpty);
}
Visual Studio 2012 报告:
error C2893: Failed to specialize function template 'std::enable_if<has_empty<T>::value>::type foo(const T &)'
1> With the following template arguments:
1> '<unnamed-type-hasEmpty>'
(注意,我真的喜欢here 中描述的这个测试的新 C++11 版本,但是 VS2012 还不支持 constexpr。)
【问题讨论】:
-
为什么使用未命名的结构?
-
哦,儿子……这是布尔值。对不起,伙计们,显然是一个愚蠢的问题。
标签: c++ templates template-meta-programming enable-if