c++ - 对于构造函数,{} 是否与 =default 相同?

标签 c++ c++11

<分区>

在默认构造函数的定义中,= default; 是否与 { } 完全相同?在某些情况下它们可能具有不同的含义吗?

示例伪代码:

template<typename... Args>
struct S // maybe with base classes
{
    S() = default;    
    // S() {}    - same or different?

    // other stuff...
};

最佳答案

它们是不同的。通过 S() {}S 被认为具有用户提供的构造函数,而 S() = default; 则没有。例如,这会导致 S 是否符合聚合类型的条件。参见 here .

关于c++ - 对于构造函数,{} 是否与 =default 相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29483044/

相关文章:

c++ - 不允许在 C++ 中使用默认构造函数

c++11 线程不修改相同的值

c++ - 正则表达式无法从字符串中提取双参数子字符串

c++ - 从 uint32_t 转换为 wchar_t 并存储在 wstring 中时发生访问冲突

C++ Visual Studio 运行时错误

c++ - Unresolved external cv::flip cv::imshow

c++ - VC++ : Performance drop x20 when more threads than cpus but not under g++

c++ - 如何在 bazel 中设置动态加载的库依赖项?

c++ - 如何 list.sort(member Function);?

c++ - 如何确保一个类仅派生自预定义的一组派生类?