I do not understand why the following code does not compile. I get the same errors using both GCC and Clang. Can someone explain or point to a part of the standard that would explain why p1 and p2 are not the same type?
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);
}
This is the output of 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);
And clang:
$ 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.
From the spec, §14.4, two types are equivalent if...
— their corresponding template template-arguments refer to the same template.
But you have two different alias templates (§14.5.7). They are not type aliases.
- A template-declaration in which the declaration is an alias-declaration (Clause 7) declares the identifier to be a alias template. An alias template is a name for a family of types. The name of the alias template is a template-name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With