youtube Channel: Advanced C++

I am creating a series of video sessions to demonstrate medium-to-advanced C++ programming techniques that I've learned over the years. Lot's of the ideas came from the book "Effective C++". It's a work in progress. My goal is to make it a comprehensive collection of advanced C++ programming video demo, so that future programmer can benefit from it.

You are cordially welcomed to watch a few of the videos and give me some feedback on how I can improve them in the future. And hopefully you can get something from the video for yourself, too.

Here is the link to the playlist:
http://www.youtube.com/playlist?list=PLE28375D4AC946CC3
Are you going to cover topics like "name mangling"? It's nice to know how the compiler handles things like that.

I'll watch a few of the videos and edit this post with my feedback.
Last edited on
I just watched part of the first one out of curiosity.

You indicate that a copy constructor will not be automatically generated by the compiler in the case of the class having a reference or a const data member. That isn't true. You also define a copy constructor as doing "member by member copying" which is a not quite accurate and may be the source of your confusion. The copy constructor performs member-by-member initialization.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Owner {};

class Dog
{
    Owner & owner ;
public:

     Dog( Owner & o) : owner(o) {}
};

int main()
{
    Owner o;
    Dog a(o) ;
    Dog b(a) ;
}


works just fine, for instance. This will disable the automatic generation of a copy assignment operator and default constructor, of course.
closed account (zb0S216C)
cire wrote:
"This will disable the automatic generation of a copy assignment operator and default constructor, of course."

No, it won't. A compiler will generate a copy-constructor, default constructor, and assignment operator, only if they are required. If you overload just 1 of three, the other two are generated (if they're used). If the compiler did not generate any of the three, even if you needed it, you'd know about it.

Wazzak
Last edited on
No, it won't


Yes, it will.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Owner {};

class Dog
{
    Owner & owner ;
public:

     Dog( Owner & o) : owner(o) {}
};

int main()
{
    Owner o;
    Dog a(o) ;
    Dog b(a) ;

    Dog c ;  // compiler is unable to generate a default constructor.
    
    b = c ;   // compiler is unable to generate an assignment operator.
}



Result compiling with VS2012:
Error	1	error C2512: 'Dog' : no appropriate default constructor available
Error	2	error C2582: 'operator =' function is unavailable in 'Dog'	


Feel free to try it on the compiler of your choice.
closed account (zb0S216C)
In your code, it won't generate them because of the initialisation rules of references. This does not disable their generation, but simply will not compile, because of the reference.

Wazzak
Last edited on
In your code, it won't generate them because of the initialisation rules of references


Exactly. Their automatic generation is disabled.
closed account (zb0S216C)
Again, not disabled, but unable to generate their definition. Disabling their generation would mean skipping member-by-member initialisation, which is not the correct behaviour. Besides, the C++ standards states that the three must be generated, even if they cause an error.

Wazzak
Last edited on
12.1.5:
...
A defaulted default constructor for class X is defined as deleted if:

— X is a union-like class that has a variant member with a non-trivial default constructor,
— any non-static data member with no brace-or-equal-initializer is of reference type,
— any non-variant non-static data member of const-qualified type (or array thereof) with no brace-orequal-
initializer does not have a user-provided default constructor,
— X is a union and all of its variant members are of const-qualified type (or array thereof),
— X is a non-union class and all members of any anonymous union member are of const-qualified type
(or array thereof),
— any direct or virtual base class, or non-static data member with no brace-or-equal-initializer, has class
type M (or array thereof) and either M has no default constructor or overload resolution (13.3) as applied
to M’s default constructor results in an ambiguity or in a function that is deleted or inaccessible from
the defaulted default constructor, or
— any direct or virtual base class or non-static data member has a type with a destructor that is deleted
or inaccessible from the defaulted default constructor.


Their automatic generation is disabled.

Edit:

Going a little deeper, however...
8.4.2.4:
Explicitly-defaulted functions and implicitly-declared functions are collectively called defaulted functions, and the implementation shall provide implicit definitions for them (12.1 12.4, 12.8), which might mean defining them as deleted.


Which does show that the definitions are generated, however there is no practical difference from a programmer's point of view other than, perhaps, the text of the error message generated by the compiler.
Last edited on
closed account (zb0S216C)
I stand corrected. Apologies, cire.

Wazzak
Nice catch, cire. I'll update it in my next version of the video.
Topic archived. No new replies allowed.