xmllmx
xmllmx

Reputation: 42277

Why does VC++ compile the code while clang doesn't?

I use VS 2015 (Update 3) to compile the following code:

#include <codecvt>
#include <cctype>
#include <functional>

int main()
{
    std::function<int(int)> fn = std::isspace;
}

If I use VC++ to compile it, it's ok. However, if I change the compiler to Visual Studio 2015 - Clang with Microsoft CodeGen (v140_clang_c2) in Visual Studio, clang reports an error:

main.cpp(7,26): error : no viable conversion from '' to 'std::function'

std::function fn = std::isspace;

More surprising, if I comments the first line as follows, clang will also be ok.

//#include <codecvt> // now clang feels happy
#include <cctype>
#include <functional>

int main()
{
    std::function<int(int)> fn = std::isspace;
}

What's the root cause?

Upvotes: 4

Views: 214

Answers (2)

user657267
user657267

Reputation: 21000

std::isspace is ambiguous, it can either refer to the function found in <cctype> which is for compatibility with C, or the function template found in <locale>.

You can resolve the ambiguity with

std::function<int(int)> fn = static_cast<int(*)(int)>(std::isspace);

Or by omitting the std:: namespace, although technically there's no requirement for implementations to import the C functions into the global namespace.

The Clang and GCC implementations of <codecvt> both seem to include declarations of the template from <locale>, hence the error; presumably VS doesn't.

Upvotes: 4

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145269

std::isspace is overloaded in the standard library.

Due to the structure of their standard library headers, one compiler sees two different declarations of the name.

Then its use without arguments or casting is ambiguous.

Upvotes: 6

Related Questions