小编Que*_*nUK的帖子

为什么构造函数调用依赖于默认析构函数的存在?

在下面的代码中,我得到两个构造函数调用Test u = "u";.但是,如果我注释掉析构函数,那么我只得到一个构造函数调用.这是为什么?

#include <iostream>

template<class T>
auto operator<<(std::ostream& os, const T& t) -> decltype(t.print(os), os) 
{ 
    t.print(os); 
    return os; 
} 

class Test 
{
public:
    template<typename T>
    Test(T&& t)
    {
        std::cout << "Test " << t << '\n';
    }
    ~Test() = default; // if commented out removes one construction
    void print(std::ostream& os) const
    {
        os << "[with T = Test]";
    }
};

int main()
{
    Test u = "u"; // two constructors (second, a temporary, with T …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
110
查看次数

标签 统计

c++ ×1