Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

= delete - what does this do?

1 view
Skip to first unread message

amarzum...@gmail.com

unread,
Jul 22, 2008, 5:48:25 PM7/22/08
to
I was looking at the std thread class for c++:

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! ]

Mathias Gaunard

unread,
Jul 22, 2008, 11:02:40 PM7/22/08
to
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.

Alberto Ganesh Barbati

unread,
Jul 23, 2008, 8:29:44 AM7/23/08
to
amarzum...@gmail.com ha scritto:

> I was looking at the std thread class for c++:
>
> 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;
>

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

ManicQin

unread,
Jul 23, 2008, 9:28:21 AM7/23/08
to
On Jul 23, 5:02 am, Mathias Gaunard <loufo...@gmail.com> wrote:

> 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?

Francis Glassborow

unread,
Jul 23, 2008, 9:28:19 AM7/23/08
to
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

Francis Glassborow

unread,
Jul 23, 2008, 9:23:35 PM7/23/08
to
Alberto Ganesh Barbati wrote:
> amarzum...@gmail.com ha scritto:
>> I was looking at the std thread class for c++:
>>
>> 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;
>>
>
> 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
>
small nit, it is 'delete' not 'deleted' (that would be a new keyword)


--
Note that robinton.demon.co.uk addresses are no longer valid.

ManicQin

unread,
Jul 24, 2008, 7:34:25 AM7/24/08
to
On Jul 24, 3:23 am, Francis Glassborow
<francis.glassbo...@btinternet.com> wrote:

>
> 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 }

--

0 new messages