简体   繁体   English

为什么以下代码使用MSVC ++编译?

[英]Why does the following code compile with MSVC++?

struct X{};

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

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

Why, just why ? 为什么, 为什么 There is no operator== defined anywhere ! 任何地方都没有operator==

I really want to understand what's going on here, to provide a detailed bug report on MS Connect. 我真的想了解这里发生了什么,提供有关MS Connect的详细错误报告。 My journey to insanity began around here in the Lounge<C++> chat room... 我的精神错乱的旅程大约始于这里在休息室<C ++>聊天室...

(Note: Neither GCC nor Clang accept this code.) (注意: GCC和Clang都不接受此代码。)

Oh, and btw, adding a private X(int) ctor causes the compilation to fail: 哦,顺便说一句,添加私有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);
}

Output: 输出:

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'

What version of MS VC++ are you using? 您使用的是什么版本的MS VC ++?

For whatever it may be worth, VC++11 Beta rejects your code with: 无论它有什么价值,VC ++ 11 Beta拒绝你的代码:

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

I'm not sure that's what I'd call the most helpful or informative error message ever, but it is rejecting the code. 我不知道这就是我所称之为最有帮助或信息的错误消息, 拒绝代码。

Under the circumstances, I'd guess filing a bug report is probably going to accomplish little (if anything). 在这种情况下,我认为提交错误报告可能会完成很少(如果有的话)。 The response I'd expect would be essentially: "Already fixed in VC++11. Upgrade when you can." 我期望的反应基本上是:“已经在VC ++ 11中得到修复。尽可能升级。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM