| |

VerySource

 Forgot password?
 Register
Search
View: 1316|Reply: 11

Ask basic questions for a template

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-7 23:40:01
| Show all posts |Read mode
Template <class _tt>
    class ……
Template <Type _tt>
    class ...
In the above two usages, the types are defined using class and Type respectively. Is there any difference?
Reply

Use magic Report

0

Threads

57

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-1-8 23:27:01
| Show all posts
No difference in use
Reply

Use magic Report

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-9 01:18:01
| Show all posts
I remember using class and typename, type has never been seen before
Reply

Use magic Report

0

Threads

19

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-1-9 12:09:02
| Show all posts
In fact, Type needs only one #define Type typename
Reply

Use magic Report

0

Threads

41

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-1-9 12:36:01
| Show all posts
It ’s no difference, it ’s just my personal habits
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-9 14:09:01
| Show all posts
The use of class will lead to a misunderstanding. It is considered that the type used by the template is limited to the class, which is also an atomic type such as int.
typename style is better!
Reply

Use magic Report

1

Threads

19

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-1-10 09:36:01
| Show all posts
Agree upstairs,

These two are the same in function
Reply

Use magic Report

1

Threads

19

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-1-10 11:09:02
| Show all posts
Agree upstairs,

These two are the same in function
Reply

Use magic Report

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-10 22:09:01
| Show all posts
The typename has a double meaning. There is no difference between the two types of usage mentioned in the landlord, but if
template <typename C>
void print2nd (const C&container)
{
    if (container.size ()> = 2) {
        typename C :: const_iterator iter (container.begin ()); // It must be here, but only add
                                                           // typename
        ........
    }
}
The reason is that at any time you want to refer to a nested dependent type name in the template, you must put the typename immediately before it. For details, please refer to effective c ++ clause 42.
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-17 23:27:01
| Show all posts
Thank you in advance.

Is it possible to specify a default value for a type specified by typename, how to do it?
is this correct
template <typename _type = string>
     class ………………
Fancy thinking, ◎ please master point ~
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list