类模板参数推导失败会导致替换失败
Class template argument deduction fails leads to substitution failure
我有一个简单的程序,我正试图用它来测试C++17的类模板参数推导。
#include <iostream>
#include <list>
int main(int argc, const char * argv[]) {
const char* a = "Hello";
std::list x(1, a);
return 0;
}
我想用std::list来推导类型为const char*
的列表。然而,当尝试运行此代码时,我得到了错误No viable constructor or deduction guide for deduction of template arguments of 'list'
。具体来说,应该与该list(size_type __n, const value_type& __x);
匹配的构造函数报告一个错误,称:
Candidate template ignored: substitution failure [with _Tp = const char *, _Alloc = std::__1::allocator<const char *>]: 'size_type' is a protected member of 'std::__1::__list_imp<const char *, std::__1::allocator<const char *> >'
我很好奇为什么这不起作用,但像这样的程序是完全正确的,std::pair
能够很容易地推导出参数:
#include <iostream>
#include <list>
int main(int argc, const char * argv[]) {
const char* a = "Hello";
std::pair x(1, a);
return 0;
}
谢谢。
clang 5和6以及gcc 7和8编译代码时不会出现问题。因此,您使用的要么是没有正确实现推导指南的编译器,要么是没有std::list
的适当推导指南的库
相关文章:
- 模板参数替换失败,并且未完成隐式转换
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- 要求子句中不允许哪些替换失败?
- 模板参数推导/替换失败,lambda作为函数指针
- 类模板参数推导失败会导致替换失败
- C++ 带有 decltype 的 SFINAE:替换失败成为错误?
- 折叠表达式模板参数推导/替换失败
- 使用"std::function"和先前推断的模板参数替换失败 - 为什么?
- 模板参数推导/替换失败 C++
- 当Boost ::绑定模板函数时,模板参数扣除/替换失败
- SFINAE使用演绎,但用替换失败
- 为什么代码中的模板参数推导/替换失败?-.
- GCC 模板参数推断/替换失败
- 为什么模板参数推导/替换失败
- 模板模板参数的替换失败
- C++模板参数推导/替换失败
- C++模板参数推导/替换失败:
- 使用类型名参数时,模板参数推导/替换失败
- 有没有办法将模板的替换失败转换为布尔值(真/假)或标签(标准::true_type/标准::false_type)
- 模板参数扣除/替换使用Lambda表达式使用高阶函数失败