【发布时间】:2018-06-24 09:56:02
【问题描述】:
我有以下相当简单的函数模板:
template <class OrderedSetType, template<class> class SupersetType>
OrderedSetType f(const SupersetType<OrderedSetType>& superset)
{
return OrderedSetType();
}
它是这样称呼的:
f(std::vector<std::string>());
并且编译器无法推导出模板参数。诊断消息不是特别有用:
<source>: In function 'int main()':
<source>:12:33: error: no matching function for call to 'f(std::vector<std::__cxx11::basic_string<char> >)'
f(std::vector<std::string>());
^
<source>:5:16: note: candidate: template<class OrderedSetType, template<class> class SupersetType> OrderedSetType f(const SupersetType<OrderedSetType>&)
OrderedSetType f(const SupersetType<OrderedSetType>& superset)
^
<source>:5:16: note: template argument deduction/substitution failed:
<source>:12:33: note: template parameters of a template template argument are inconsistent with other deduced template arguments
f(std::vector<std::string>());
^
为什么会出现错误? GCC 7.3 与-std=c++14 发生,-std=c++17 不会发生。 C++ 17 标准中的哪些更改允许此代码编译?我可以让它为 C++14 编译吗?
这是现场演示:https://godbolt.org/g/89BTzz
顺便说一下,明确指定模板参数并没有帮助。
P。 S. 同时MSVC对这段代码没有问题,但是clang 5和6即使在C++17模式下也无法编译。因此,要么 clang 有一个 bug 并且无法编译符合标准的代码,要么 GCC 有一个 bug 并成功编译了它不应该的代码(-std=c++17)。
【问题讨论】:
标签: c++ c++14 c++17 template-argument-deduction template-templates