Следующий код компилируется и работает просто отлично на gcc 4.9.3 и clang 3.7.1
// std::unique_ptr
#include <memory>
// Template class for template-template arguments
template <typename Real>
struct Bar {};
// Base class
template <typename T,template <typename> class XX>
struct Base {};
// Derived class that operates only on Bar
template <typename Real>
struct Derived : public Base <Real,Bar> {};
// Holds the unique_ptr
template <typename T,template <typename> class XX>
struct Foo {
std::unique_ptr <Base <T,XX>> foo;
};
// Create an alias template
template <typename Real>
using Buz = Bar <Real>;
int main() {
#if 0
auto f = Foo <double,Buz> (); //Causes error!
#else
auto f = Foo <double,Bar> ();
#endif
f.foo = std::make_unique <Derived <double>> (Derived <double>());
}
Однако, если мы изменим #if 0
на #if 1
, gcc компилирует, но clang не делает:
g++ -std=c++14 test03.cpp -o test03_gcc
clang++ -std=c++14 test03.cpp -o test03_clang
test03.cpp:32:11: error: no viable overloaded '='
f.foo = std::make_unique <Derived <double>> (Derived <double>());
~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/unique_ptr.h:249:7: note:
candidate function not viable: no known conversion from
'unique_ptr<Derived<double>, default_delete<Derived<double>>>' to
'unique_ptr<Base<double, Buz>, default_delete<Base<double, Buz>>>' for
1st argument
operator=(unique_ptr&& __u) noexcept
^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/unique_ptr.h:278:7: note:
candidate function not viable: no known conversion from 'typename
_MakeUniq<Derived<double> >::__single_object' (aka
'unique_ptr<Derived<double> >') to 'nullptr_t' for 1st argument
operator=(nullptr_t) noexcept
^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/unique_ptr.h:357:19: note:
candidate function not viable: no known conversion from
'unique_ptr<Derived<double>, default_delete<Derived<double>>>' to
'const unique_ptr<Base<double, Buz>, default_delete<Base<double,
Buz>>>' for 1st argument
unique_ptr& operator=(const unique_ptr&) = delete;
^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/include/g++-v4/bits/unique_ptr.h:264:22: note:
candidate template ignored: disabled by 'enable_if' [with _Up =
Derived<double>, _Ep = std::default_delete<Derived<double> >]
typename enable_if< __and_<
^
1 error generated.
Makefile:2: recipe for target 'all' failed
make: *** [all] Error 1
В чем проблема с использованием шаблона псевдонима в этом контексте? Или, если gcc более разрешительна, чем это должно быть, почему это так?