Compilation error using template specialization, default arguments, and VS2013

template<typename T>
void f(const T &v = T());

template<>
void f<std::string>(const std::string &v)
{
    std::cout << v;
}

int main(int argc, char* argv[])
{
    f<std::string>(); // Error in VS2013,  OK in VS2012, gcc-4.7
    f<std::string>("Test");   // OK
    f<std::string>(std::string());  //OK
    return 0;
}

The latest Visual Studio 2013 compiler gives the following compiler error for the case where the default argument should be used:

error C2440: 'default argument' : cannot convert from 'const std::string *' to 'const std::string &'
Reason: cannot convert from 'const std::string *' to 'const std::string'
No constructor could take the source type, or constructor overload resolution was ambiguous

Visual Studio 2012 and gcc-4.7 compile in order.

Update. It seems to be a VS2013 error, are there temporary workarounds that do not require significant code changes until this is fixed by MS? An error report has been sent to MS connect.

+4
source share
1 answer

, , ( )...

template<typename T>
struct foo
{
  static void f(const T &v = T());
};

template<>
struct foo<std::string>
{
  static void f(const std::string &v = std::string())
  {
    std::cout << v;
  }
};

, Visual Studio 2013, , , .

, ,

foo<std::string>::f()
foo<std::string>::f("Text")

:

template<typename T>
void f_wrapper(const T &v = T())
{
  foo<T>::f(v);
}
+1

All Articles