Re: = delete - what does this do?
Mathias Gaunard wrote:
On 22 juil, 23:48, amarzumkhaw...@gmail.com wrote:
I was looking at the std thread class for c++:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html#th...
I was wondering what the "= delete" does when declaring a constructor?
--> thread(const thread&) = delete;
It is used to delete constructs that are generated by default, such as
the copy constructor and the assignment operator.
well sort of :) It is a new syntax for C++0x that informs the compiler
that the declared function (can be any function, not just a ctor) will
not be defined and that any time overload resolution selects that
signature it must issue a diagnostic.
It has considerable advantage over the current hack which can sometimes
delay diagnosis till link time. One advantage is that it makes the
programmer's intent explicit and so prevents some well meaning fool
adding a definition.
The syntax also includes =default which has a more limited use in that
it tells the compiler to generate the default definition in cases where
it would otherwise have been suppressed, or where the programmer would
have had to provide a definition:
class mytype {
public:
mytype() = default; // always generate a default ctor
mytype(mytype &) = default; // always generate this copy ctor
mytype(mytype const &) = delete;
// attempts to copy a const mytype are erroneous
void foo(int);
void foo(long) = delete; // conversion from long to int not allowed
etc.
};
int bar(char *) = delete;
int bar(std::string const &) ;
bar() cannot be called with a char *, nor may a char* be converted to a
std::string
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]