gpt4 book ai didi

c++ - 为什么下面的代码可以用 MSVC++ 编译?

转载 作者:可可西里 更新时间:2023-11-01 15:22:36 24 4
gpt4 key购买 nike

struct X{};

template<class T>
decltype(X() == int()) f(T const&){ return true; }

int main(void) {
X x;
f(x);
}

为什么,只是为什么任何地方都没有定义operator==!

我真的很想了解这里发生了什么,以便提供有关 MS Connect 的详细错误报告。我的精神错乱之旅始于 here在 Lounge 聊天室...

(注意:GCC 和 Clang 都不接受此代码。)

哦,顺便说一句,添加私有(private) X(int) 构造函数会导致编译失败:

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'

最佳答案

您使用的是什么版本的 MS VC++?

无论值(value)如何,VC++11 Beta 都会拒绝您的代码:

trash.cpp(8): error C2893: Failed to specialize function template ''unknown-type' f(const T &)'
With the following template arguments:
'X'

我不确定这是否是我所说的最有帮助或信息最丰富的错误消息,但它拒绝代码。

在这种情况下,我猜提交错误报告可能收效甚微(如果有的话)。我期望的响应基本上是:“已在 VC++11 中修复。尽可能升级。”

关于c++ - 为什么下面的代码可以用 MSVC++ 编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10695148/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com