C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 使用模板特化,默认参数和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;
}

最新的Visual studio 2013编译器在必须使用认参数的情况下给出以下编译器错误

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和gcc-4.7编译正常.

更新:因为它似乎是一个VS2013错误,是否有任何临时解决方法,在MS修复此问题之前不需要进行大量代码更改?错误报告已在MS connect上提交.

解决方法

每当我看到模板功能出现这种问题时,我都会尝试切换到模板结构(如果需要临时解决方法)……

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);
}

大佬总结

以上是大佬教程为你收集整理的c – 使用模板特化,默认参数和VS2013编译错误全部内容,希望文章能够帮你解决c – 使用模板特化,默认参数和VS2013编译错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。