Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can any function be a deleted-function?

The working draft explicitly calls out that defaulted-functions must be special member functions (eg copy-constructor, default-constructor, etc, (§8.4.2.1-1)). Which makes perfect sense.

However, I don't see any such restriction on deleted-functions(§8.4.3). Is that right?

Or in other words are these three examples valid 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;
like image 379
deft_code Avatar asked May 20 '10 15:05

deft_code


People also ask

What is a deleted function?

Deleted function declaration is a new form of function declaration that is introduced into the C++11 standard. To declare a function as a deleted function, you can append the =delete; specifier to the end of that function declaration. The compiler disables the usage of a deleted function.

Will delete operator works for normal function?

= delete can be used for any function, in which case it is explicitly marked as deleted and any use results in a compiler error.

Will delete operator works for normal function in C++?

Prior to C++ 11, the operator delete had only one purpose, to deallocate a memory that has been allocated dynamically. The C++ 11 standard introduced another use of this operator, which is: To disable the usage of a member function.

What does delete mean C++?

Delete is an operator that is used to destroy array and non-array(pointer) objects which are created by new expression. Delete can be used by either using Delete operator or Delete [ ] operator. New operator is used for dynamic memory allocation which puts variables on heap memory.


1 Answers

The C++0x spec (§[dcl.fct.def.delete]) doesn't deny such constructs, and g++ 4.5 recognize all 3 of them.

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
like image 59
kennytm Avatar answered Sep 24 '22 14:09

kennytm