保证复制省略的构造函数实例化 [英] Constructor instantiation in a world of guaranteed copy elision

查看:66
本文介绍了保证复制省略的构造函数实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下示例:

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

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

A<int> f();
A<int> g() { return f(); }

由于 int 没有嵌套的 type typedef。但是,为什么要实例化该构造函数呢? f()是与 g()的返回值相同类型的prvalue,甚至不应搬到那里。是什么导致我们实例化错误的构造函数?

Neither gcc nor clang compile this code due to int not having a nested type typedef. But why is that constructor being instantiated at all? f() is a prvalue of the same type as the return of g(), there shouldn't even be a move there. What is causing us to instantiate the bad constructor?

推荐答案

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

The constructor is a bit of a red herring. The same would happen if it was any other member function.

template <typename T>
struct A
{
    void foo(type<T>); // Same error
};

这是由于 [temp.inst] / 2


类模板特化的隐式实例化导致
声明的隐式实例化,但不会导致
定义,默认参数或类
成员函数的noexcept指定符隐式实例化[.。 。]

The implicit instantiation of a class template specialization causes the implicit instantiation of the declarations, but not of the definitions, default arguments, or noexcept-specifiers of the class member functions, [...]

实例化声明,因此 type< T> 必须格式正确。

The declaration is instantiated, so type<T> has to be well-formed.

这篇关于保证复制省略的构造函数实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆