c++ - 任何函数都可以是删除函数吗?

标签 c++ c++11 deleted-functions

工作草案明确指出默认函数 必须是特殊成员函数(例如复制构造函数、默认构造函数等,(§8.4.2.1-1))。这非常有道理。

但是,我没有看到任何关于已删除函数(§8.4.3)的限制。是吗?

或者换句话说,这三个示例是否有效 c++0

struct Foo
{
   // 1
   int bar( int ) = delete;
};


// 2
int baz( int ) = delete;


template< typename T >
int boo( T t );

// 3
template<>
int boo<int>(int t) = delete;

最佳答案

C++0x 规范 (§[dcl.fct.def.delete]) 不拒绝此类构造,并且 g++ 4.5 识别所有 3 个构造。

x.cpp: In function 'int main()':
x.cpp:4:8: error: deleted function 'int Foo::bar(int)'
x.cpp:21:11: error: used here
x.cpp:9:5: error: deleted function 'int baz(int)'
x.cpp:22:2: error: used here
x.cpp:9:5: error: deleted function 'int baz(int)'
x.cpp:22:8: error: used here
x.cpp:17:5: error: deleted function 'int boo(T) [with T = int]'
x.cpp:23:7: error: used here

关于c++ - 任何函数都可以是删除函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2875333/

相关文章:

c++ - sets、multisets、maps 和 multimaps 如何在内部工作

c++ - 标准与显式自动推导变量声明

c++ - 如何在 C++ 正则表达式中匹配带有左大括号 { 的字符串

c++ - 删除转换运算符

c++ - C++ 中何时需要#include <new> 库?

c++ - SDL——面向对象的方式

c++11 - SIGINT 未在此范围内声明

c++ - 删除复制构造函数导致删除默认构造函数

c++ - 为什么删除的复制构造函数不允许使用其他具有多态类型的构造函数?

c++ - 用 Poco :Condition 唤醒两个线程