Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

I really want to understand what's going on here, to provide a detailed bug report on MS Connect. My journey to insanity began around here in the Lounge<C++> chat room...

(Note: Neither GCC nor Clang accept this code.)

Oh, and btw, adding a private X(int) ctor causes the compilation to fail:

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'
like image 896
Xeo Avatar asked May 22 '12 02:05

Xeo


People also ask

What compiler does MSVC use?

Microsoft C++ Compiler (MSVC) This is the default compiler for most Visual Studio C++ projects and is recommended if you are targeting Windows.

Does MSVC compile C?

Microsoft C/C++ (MSVC) is a C and C++ compiler that, in its latest versions, conforms to some of the latest C language standards, including C11 and C17.

Can MSVC cross compile?

Build supports cross compilation with the gcc and msvc toolsets. For the complete list of allowed opeating system names, please see the documentation for target-os feature. When using the msvc compiler, it's only possible to cross-compiler to a 64-bit system on a 32-bit host.

Where is MSVC compiler installed?

More precisely, the default path where you'll find the compiler is C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin .


1 Answers

What version of MS VC++ are you using?

For whatever it may be worth, VC++11 Beta rejects your code with:

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."

like image 124
Jerry Coffin Avatar answered Nov 03 '22 09:11

Jerry Coffin