Is this valid c++?

D

dec4106

Earlier today I came across a code example that looked something like
this:


template <typename T, unsigned S>
class MyClass
{
public:
MyClass() = default;
...

"default" is not defined anywhere in the example. I've never seen an
expression like this before and don't understand how it could work,
and haven't been able to make it work. Is this a C++11 thing?
 
R

Richard Damon

Earlier today I came across a code example that looked something like
this:


template<typename T, unsigned S>
class MyClass
{
public:
MyClass() = default;
...

"default" is not defined anywhere in the example. I've never seen an
expression like this before and don't understand how it could work,
and haven't been able to make it work. Is this a C++11 thing?

Yes, that is a new C++11 construct, it tells the compiler to make a
standard default constructor MyClass() with the "default"
implementation, even if some other constructor is defined that would
suppress the generation of the default constructor.
 
A

Alain Ketterlin

dec4106 said:
template <typename T, unsigned S>
class MyClass
{
public:
MyClass() = default;
...

"default" is not defined anywhere in the example. I've never seen an
expression like this before and don't understand how it could work,
and haven't been able to make it work. Is this a C++11 thing?

Yes, it lets the compiler generate the ctor for you, with default
behavior.

-- Alain.
 
J

Jorgen Grahn

Yes, that is a new C++11 construct, it tells the compiler to make a
standard default constructor MyClass() with the "default"
implementation, even if some other constructor is defined that would
suppress the generation of the default constructor.

I hope this doesn't become the norm -- mixing C++11 into examples,
discussions etc -- just yet. I bet most of us cannot switch, due
to having to support some system which doesn't have a bleeding edge
compiler yet. (My Debian Stable systems have gcc 4.4, which
implements maybe 50%.)

C++11 is great, and it seems it will propagate out to the end users
much faster than C++98 did ... but we have to survive while waiting
for it, too.

/Jorgen
 
V

Victor Bazarov

I hope this doesn't become the norm -- mixing C++11 into examples,
discussions etc -- just yet. I bet most of us cannot switch, due
to having to support some system which doesn't have a bleeding edge
compiler yet. (My Debian Stable systems have gcc 4.4, which
implements maybe 50%.)

C++11 is great, and it seems it will propagate out to the end users
much faster than C++98 did ... but we have to survive while waiting
for it, too.

And how long do you think we should wait?

V
 
J

Juha Nieminen

Krice said:
What is that default behaviour?

All member objects are default-constructed (after which the derived class
default constructor is called if this was really an object of a derived
class).
 
J

Jorgen Grahn

And how long do you think we should wait?

I won't know until it happens (and I'll make sure to tell you).

It's subjective of course; I'm just saying I think we're not there yet,
for e.g. the reasons I listed above.

/Jorgen
 
V

Victor Bazarov

I won't know until it happens (and I'll make sure to tell you).

It's subjective of course; I'm just saying I think we're not there yet,
for e.g. the reasons I listed above.

<shrug> You're not there. I'm not there. Shouldn't preclude those who
want to discuss C++11 from talking about it and giving code snippets
with the language _a Standard for which has already been approved_.

The more "we" (as a c.l.c++ community) do the "mixing C++11 into
examples", the sooner it is learned by "us" (and that means you, as well).

I say, go for it! As an old Russian saying goes, to learn to swim one
must go into water.

V
 
J

Jorgen Grahn

<sarcasm> We'll all wait with baited breath. </sarcasm>

I think you missed /my/ sarcasm above ;-)
<shrug> You're not there. I'm not there. Shouldn't preclude those who
want to discuss C++11 from talking about it and giving code snippets
with the language _a Standard for which has already been approved_.

Of course C++11 needs to be discussed et cetera like you say; I don't
propose to treat it as a second-class language for N more years.

I'm just afraid that while the early adopters are having fun with the
new features (and all the positive effects which come with that), the
big crowd of C++ users are partly left behind, some of them being
/more/ confused than they already were.
The more "we" (as a c.l.c++ community) do the "mixing C++11 into
examples", the sooner it is learned by "us" (and that means you, as well).

I say, go for it! As an old Russian saying goes, to learn to swim one
must go into water.

I'd like to, but my code has to compile & run on a few different
machines not under my direct control. When g++ 4.6 hits Debian Stable
is when I personally can consider flipping the -std=c++11 switch, or
whatever it's called. By that time, I expect most systems which have
C++98 today, will also have large parts of C++11 (because Debian isn't
known for it's bleeding-edge-ness).

(At work is another issue: as is often the case, I'm stuck with even
older tools there. But that's not new; I can't even use C99 there.)

/Jorgen
 
D

Daryle Walker

Earlier today I came across a code example that looked something like
this:

template <typename T, unsigned S>
class MyClass
{
public:
    MyClass() = default;
    ...

"default" is not defined anywhere in the example.  I've never seen an
expression like this before and don't understand how it could work,
and haven't been able to make it work.  Is this a C++11 thing?

I think this is from one of my posts.

Ironically, considering the rest of this thread, I don't even have a C+
+11 compiler (or a system ready to use one). I'm learning C++11 from
web sites and Usenet posts.

I, nor anyone else, can define "default" in an example since it's a
keyword!

Daryle W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top