http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html#thread.threads
I was wondering what the "= delete" does when declaring a constructor?
--> thread(const thread&) = delete;
Thanks
{ See the paper titled "Defaulted and Deleted Functions" at
<http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2326.html>.
-mod/sk }
--------
class thread
{
public:
// types:
class id;
typedef implementation-defined native_handle_type; // See
[thread.native]
// construct/copy/destroy:
thread();
template <class F> explicit thread(F f);
template <class F, class ...Args> thread(F&& f, Args&&... args);
~thread();
thread(const thread&) = delete;
thread(thread&&);
thread& operator=(const thread&) = delete;
thread& operator=(thread&&);
// members:
void swap(thread&&);
bool joinable() const;
void join();
void detach();
id get_id() const;
native_handle_type native_handle(); // See [thread.native]
// static members:
static unsigned hardware_concurrency();
};
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
It is used to delete constructs that are generated by default, such as
the copy constructor and the assignment operator.
The "= delete" is a new syntax that will be introduced in C++0x to
declare that a certain function will *not* be provided. It can be used
to suppress the implicit generation of the copy constructor (as in this
case) or assignment operator, replacing the common hackish idiom of
declaring them private. This allows the compiler to provide a more
meaningful diagnostic.
The = deleted can be used on any function, including non-member
functions. For example:
void f(double x) { /* ... */ }
void f(int) = deleted;
f(1.0); // ok
f(1); // error: function is deleted
without the deleted function, f(1) would have been converted 1 to 1.0
and f(double) would be called.
HTH,
Ganesh
> It is used to delete constructs that are generated by default, such as
> the copy constructor and the assignment operator.
It's a proposal for the new std not the old right?
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
--
Note that robinton.demon.co.uk addresses are no longer valid.
>
> small nit, it is 'delete' not 'deleted' (that would be a new keyword)
>
I tried to find a reference to this keyword, is there any documents?
{ See <url: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2691.pdf>,
§8.4 "Function definitions" [dcl.fct.def]. - mod }
--