문제

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;
도움이 되었습니까?

해결책

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

다른 팁

I think they're all OK.

= delete is good for ensuring an overload isn't used (§8.4.3/2), which is useful outside classes.

Now 5 months later I look at the other answers… delete isn't only useful for functions with implicit definitions. It's the clean alternative to a comment saying "no implementation — using this is a linker error." It provides an explicit way to not implement something, for example a base template where only explicit specializations will actually exist. The compiler will complain before link time.

For a slightly odd, but totally reasonable example, consider

class abc {
protected:
    inline virtual ~abc() = 0;
    inline virtual void do_something() = 0;
};

abc::~abc() {}
void abc::do_something = delete;

Both = 0 and = delete can be used on the same function. Without = delete, the user can give an accidental courtesy call to abc::do_something().

I wouldn't be surprised if the next iteration of C++ after C++0x adds explicitly deleted classes.

From what I understand from the definition of "deleted" member functions, is that it applies only to special member functions (constructor, copy, assignment ) that can be created automatically by the compiler and not to ordinary member functions (which makes no sense at all IMO, to declare functions to be "deleted", so just do not declare them anyway)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top