C++ Templated struct definition

I have been researching the details of the following struct definition and I have some questions that have been hard to clarify. This came from a source focused on the C++ 11 standard.

1
2
3
4
5
6
7
8
9
10
template<class C>                                             // 1 
struct text : std::basic_string<C>                            // 2 
{                                                             // 3 
  text() : text{ "" } { }                                     // 4 
  text( char const* s ) : std::basic_string<C>( s ) { }       // 5 
  text( text&& ) = default;                                   // 6 
  text( text const& ) = default;                              // 7 
  text& operator=( text const& ) = default;                   // 8 
  text& operator=( text&& ) = default;                        // 9 
};                                                            // 10  

So, it seems that 'text' derives from 'std::basic_string'...

Lines 4 thru 7 appear to be constructors, but why are lines 6 and 7 marked as '= default'? Or, how are they both '= default' constructors? I am not exactly sure what '= default' means in this context.

On lines 1, 2, and 5, does the <C> represent 'CharT'? And, are they required for this definition to be valid (or compile successfully)?
Last edited on
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/jEywvCM9/


EDIT: OP updated their post to use code tags.

2) The default keyword was introduced in C++11. It makes it explicit that the compiler will automatically generate these constructors and operators:

https://docs.microsoft.com/en-us/cpp/cpp/explicitly-defaulted-and-deleted-functions?view=vs-2019

3) I see nothing called CharT anywhere in the code you've posted. C is the type that the class will be templated on. It is specified by, or inferred by the compiler from, the code that uses the template to create the class.
Last edited on
Thank you for the link to the Microsoft document. I did not see that page when I was searching....

I was reading online and my reference to 'CharT' comes from:
https://en.cppreference.com/w/cpp/string/basic_string/basic_string

where I assumed the 'C' in '<C>' was a placeholder....

Ok, so what is line 4, and why is is not marked '= default' ? I mean, isn't this the default constructor?

Also, if '= default' were left off of lines 6 and 7, then what would that do to this definition?

Last edited on
where I assumed the 'C' in '<C>' was a placeholder....

I'm not sure what you mean by "placeholder" here. C is the type used to create the actual class from the template. Do you understand what templates are, and how they work?

In the code you've posted, the type C is being used as the first template parameter for basic_string, which is called CharT in that cppreference page, yes.

Ok, so what is line 4, and why is is not marked '= default' ? I mean, isn't this the default constructor?

You've either not read, or not understood, the page I linked you to. You're muddling up two different uses of the word "default".

1) Default constructor means the constructor that takes no arguments. So, yes, line 4 defines the default constructor.

2) There are certain members of a class that, if we do not define them explicitly, the compiler can automatically generate them. These include:

- the default constructor
- the copy constructor
- the move constructor
- assignment operators.

There are rules about how and when the compiler does this, but declaring these members using = default makes it explicit that we want the compiler to generate these automatically.

Crucially, as you can see, on line 4 the default constructor is being explicitly defined for this class - clearly, we are not wanting the compiler to automatically generate one.

Also, if '= default' were left off of lines 6 and 7, then what would that do to this definition?

It would cause compilation linking to fail, because you've declared that you're going to define these members, but supplied no actual definitions of them.

EDIT: I've used "class" all the way through, when your code actually declares a struct. Since classes and structs are almost identical except for one obvious detail, everything I've said still applies to structs.
Last edited on
Topic archived. No new replies allowed.