小编Sar*_*yan的帖子

C++ 中的预期析构函数

我有这段代码,输出如下:

链接到以下示例 https://godbolt.org/z/z8Pn9GsTv

template <typename T>
struct A1 {
    A1() {
        std::cout << "construction of a1" << std::endl;
    }

    ~A1() {
        std::cout << "destruction of a1" << std::endl;
    }
    ~A1() requires (std::is_same_v<T,int>) {
        std::cout << "it is an int" << std::endl;
    }
};

int main() {
    A1 <int>a;
    
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

construction of a1
destruction of a1
Run Code Online (Sandbox Code Playgroud)

但交换析构函数的位置会给出其他结果:

代码链接 https://godbolt.org/z/vxj7dPqaj

template <typename T>
struct A1 {
    A1() {
        std::cout << "construction of a1" << std::endl;
    }

    ~A1() requires (std::is_same_v<T,int>) …
Run Code Online (Sandbox Code Playgroud)

c++ destructor c++20

27
推荐指数
1
解决办法
1119
查看次数

标签 统计

c++ ×1

c++20 ×1

destructor ×1