【发布时间】:2016-07-11 15:59:19
【问题描述】:
我不明白为什么下面的代码无法编译。我使用 GCC 和 Clang 都遇到了同样的错误。有人可以解释或指出标准的一部分来解释为什么 p1 和 p2 不是同一类型吗?
struct TypeT {};
struct TypeU {};
template<typename T, typename U = TypeU>
struct Foo {};
template<typename T, typename U>
struct Bar
{
};
template<typename T, template <typename> class U>
struct FooBar
{
};
template<typename T>
using FooAlias1 = Foo<T>;
template<typename T>
using FooAlias2 = Foo<T>;
template<typename T>
void DoStuff(const T& p1, const T& p2)
{
}
int main(void)
{
FooBar<TypeT, FooAlias1> p1;
FooBar<TypeT, FooAlias2> p2;
DoStuff(p1, p2);
}
这是 gcc 的输出:
$ gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
$ gcc -std=c++11 test.cpp
test.cpp: In function ‘int main()’:
test.cpp:34:19: error: no matching function for call to ‘DoStuff(FooBar<TypeT, FooAlias1>&, FooBar<TypeT, FooAlias2>&)’
DoStuff(p1, p2);
^
test.cpp:34:19: note: candidate is:
test.cpp:26:6: note: template<class T> void DoStuff(const T&, const T&)
void DoStuff(const T& p1, const T& p2)
^
test.cpp:26:6: note: template argument deduction/substitution failed:
test.cpp:34:19: note: deduced conflicting types for parameter ‘const T’ (‘FooBar<TypeT, FooAlias1>’ and ‘FooBar<TypeT, FooAlias2>’)
DoStuff(p1, p2);
还有叮当声:
$ clang --version
Ubuntu clang version 3.4-1ubuntu3 (tags/RELEASE_34/final) (based on LLVM 3.4)
$ clang -std=c++11 test.cpp
test.cpp:34:5: error: no matching function for call to 'DoStuff'
DoStuff(p1, p2);
^~~~~~~
test.cpp:26:6: note: candidate template ignored: deduced conflicting types for parameter 'T' ('FooBar<[...], template FooAlias1>'
vs. 'FooBar<[...], template FooAlias2>')
void DoStuff(const T& p1, const T& p2)
^
1 error generated.
【问题讨论】:
-
我没有标准引用,但那些别名不是类型别名,它们是模板别名,如果我没记错的话,标准在这种情况下不保证相等。