在保证副本省略的世界中的构造函数实例化

Bar*_*rry 6 c++ templates c++17

考虑这个例子:

template <typename T>
using type = typename T::type;

template <typename T>
struct A
{
    A(type<T>);
};

A<int> f();
A<int> g() { return f(); }
Run Code Online (Sandbox Code Playgroud)

由于int没有嵌套的typetypedef,gcc和clang都没有编译这段代码.但是为什么构造函数会被实例化呢?f()是一个与返回相同类型的prvalue,g()甚至不应该在那里移动.是什么导致我们实例化坏构造函数?

Sto*_*ica 9

构造函数有点像红鲱鱼.如果它是任何其他成员函数,也会发生同样的情况.

template <typename T>
struct A
{
    void foo(type<T>); // Same error
};
Run Code Online (Sandbox Code Playgroud)

这是由于[temp.inst]/2

类模板特化的隐式实例化导致声明的隐式实例化,而不是类成员函数的定义,默认参数或noexcept-specifiers,[...]

声明是实例化的,因此type<T>必须格式良好.