【发布时间】:2018-04-16 16:58:49
【问题描述】:
以下代码在 GCC 中运行良好,但在 clang 中编译失败:
#include <iostream>
#include <string>
template <typename T>
struct A {
template <typename C>
A(const C& c) { std::cout << "base" << std::endl; }
};
template <>
template <>
A<std::string>::A<std::string>(const std::string& s) {
std::cout << s << std::endl;
}
int main()
{
std::string f("foo");
A<std::string> a(f);
A<std::string> b(1.2);
}
GCC 输出:
foo
base
但是 Clang 给出以下错误:
source_file.cpp:14:17: error: out-of-line constructor for 'A' cannot have
template arguments
A<std::string>::A<std::string>(const std::string& s) {
^~~~~~~~~~~~~~
1 error generated.
我只是想确认是否有人知道 clang 是否 正确 不允许这样做,或者它是否是 clang 中的一个错误。如果我删除了显式模板参数,那么 clang 可以很好地推断模板参数,编译并产生正确的输出。只有显式模板参数才会编译失败,我想不出标准中有什么会不允许这样做。
谢谢
【问题讨论】:
标签: c++ templates language-lawyer template-specialization