【发布时间】:2012-05-28 12:54:23
【问题描述】:
struct X{};
template<class T>
decltype(X() == int()) f(T const&){ return true; }
int main(void) {
X x;
f(x);
}
为什么,只是为什么? 任何地方都没有定义operator==!
我真的很想了解这里发生了什么,以提供有关 MS Connect 的详细错误报告。我的疯狂之旅始于 Lounge
(注意:GCC 和 Clang 都不接受此代码。)
哦,顺便说一句,添加私有 X(int) ctor 会导致编译失败:
struct X{
X(){}
private:
X(int);
};
template<class T>
decltype(X() == int()) f(T const&){ return true; }
int main(void) {
X x;
f(x);
}
输出:
1>src\main.cpp(12): error C2248: 'X::X' : cannot access private member declared in class 'X'
1> src\main.cpp(4) : see declaration of 'X::X'
1> src\main.cpp(1) : see declaration of 'X'
【问题讨论】:
-
其他编译器接受吗?
-
@Billy:不,添加了该信息。背景信息:我几乎对 MSVC 在 C++ 聊天室中所做的事情发疯了,几个小时后就放弃了。
-
添加了一些背景信息。另外,匿名投反对票的人能否说出投反对票的原因?
-
您是否在实际会被计算的地方尝试过相同的表达式?只是好奇..
-
你真的是在问为什么编译器会出现错误?
标签: c++ templates visual-c++ c++11 decltype