c++11/14 make_unique对std::string的异常重载
c++11/14 make_unique ambigious overload for std::string
有人能解释一下如何解决make_unique的异常过载警告吗,错误来自哪里,它的确切含义是什么(我确实理解什么是异常过载,但我不确定为什么我会为这个特定的代码得到一个)?我使用的是c++11,因此我使用了Herb Sutter推荐的模板。
使用它我得到以下错误:
Error 4 error C2668: 'make_unique' : ambiguous call to overloaded function
visualstudio13中悬停在工具提示上给了我以下方法:
function template "std::enable_if<!std::is_array<_Ty>::value, std::unique_ptr<_Ty,std::default_delete<_Ty>>>::type std::make_unique<_Ty,_Types...>(_Types &&..._Args)"
function template "std::unique_ptr<T, std::default_delete<T>> make_unique<T,Args...>(Args...)
argument types are: std::string
第二个应该是从make_unique模板调用的
/* Will be part of c++14 and is just an oversight in c++11
* From: http://herbsutter.com/gotw/_102/
*/
template<typename T, typename ...Args>
std::unique_ptr<T> make_unique(Args&& ...args){
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
要转发到的构造函数:
Shader(const std::string& name);
生成错误的代码
std::string _name = "Shader";
std::unique_ptr<Shader> s = make_unique<Shader>(_name);
调用不明确,因为确实具有std::make_unique
,如您引用的工具提示内容所示。尽管您没有编写std::
,但由于您正在传递一个依赖于std::string
参数的查找,因此会自动搜索该命名空间。
当你说"我正在使用C++11"时,这并不完全正确,因为Visual Studio不允许你选择用哪种标准编写。它只是为你提供了它为任何给定功能所收集的最新支持。显然,Visual Studio 2013有C++14的std::make_unique
。
移除您的。
相关文章:
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- std:string::substr 异常安全吗?
- std::string::compare(const char*) 会抛出异常吗?
- Catch2 迫使我在异常中添加对 std::string 的强制转换,这会产生其他问题吗?
- 从 boost::存档:binary_iarchive 反序列化 std::string 时出现错误的分配异常
- 从可能引发异常的函数返回 std::string
- 模板类映射构建在stl :: structs的向量上 - 寻找无效的键(stl :: string)并抛出异常
- 更新了atoi(string.c_str())的偶尔未处理的异常
- c++11/14 make_unique对std::string的异常重载
- 为什么当我将 std::string 传递给 printf 时会出现异常
- 如何避免将std::string放在异常类中
- std::string异常以及如何查找已编译的c++应用程序的平台工具集
- (不是)在异常中使用 std::string
- C2061:语法错误:标识符'string' - 行为异常
- 从double到string到double的转换会引发异常
- 函数返回string.c_str()作为atoi()异常行为的参数
- 为什么 std::string::substr 会抛出异常而不是返回空字符串
- 抛出、捕获、限定stl::string.c_str()异常
- 从Qstring到std::string的转换会抛出异常