mercredi 28 janvier 2015

Is the "lazy man's enable_if" legal C++?

I frequently use a technique I call the "lazy man's enable_if," where I use decltype and the comma operator to enable a function based on some template input. Here is a small example:



template <typename F>
auto foo(F&& f) -> decltype(f(0), void())
{
std::cout << "1" << std::endl;
}

template <typename F>
auto foo(F&& f) -> decltype(f(0, 1), void())
{
std::cout << "2" << std::endl;
}


With --std=c++11, g++ 4.7+ and Clang 3.5+ happily compile that bit of code (and it works as I would expect). However, when using MSVC 14 CTP5, I get this error complaining of foo already being defined:



Error error C2995: 'unknown-type foo(F &&)': function template has already been defined c++-scratch main.cpp 15



So my question is: Is the "lazy man's enable_if" legal C++ or is this an MSVC bug?


Aucun commentaire:

Enregistrer un commentaire