template parameter that can also vary at runtime

T

Taras_96

Hi all,

I've come across the following code:

<snip>

We can generalize even further. We haven't seen the constructor for
basic_streambuf yet:

template <class charT, class traits = ios_char_traits<charT> >
class basic_streambuf {
traits traits_; // member data
...
public:
basic_streambuf(const traits& b = traits())
: traits_(b) { ... }

int_type eof() { return traits_.eof(); }
};

By adding a default constructor parameter, we can use a traits
template parameter that may vary not only at compile time, but at
runtime

</snip>

What does this code allow you to do? How does it let you change a
traits parameter that varies at runtime?
 
B

brian tyler

Sounds wrong doesn't it; if the type of charT is known at compile
time, then that completely determines the type of
ios_char_traits<charT>

I would be very surprised if you could "choose" a template parameter
at runtime, because the class / function must be created at compile
time. What is the source of the snippet? Maybe they don't mean choose
in the sense one expects.
 
O

Oncaphillis

brian said:
Sounds wrong doesn't it; if the type of charT is known at compile
time, then that completely determines the type of
ios_char_traits<charT>

I would be very surprised if you could "choose" a template parameter
at runtime, because the class / function must be created at compile
time. What is the source of the snippet? Maybe they don't mean choose
in the sense one expects.

May be they intended

<snip>
template <class charT, class traits = ios_char_traits<charT> >
class basic_streambuf {
traits & traits_; // member data. Note the '&' !
</snip>

Now if you initialize a ref you might overwrite
virtual methods in traits.

Just a assumption though.

O.
 
T

Taras_96

May be they intended

<snip>
template <class charT, class traits = ios_char_traits<charT> >
class basic_streambuf {
traits & traits_; // member data. Note the '&' !
</snip>

Now if you initialize a ref you might overwrite
virtual methods in traits.

Just a assumption though.

O.

The entire article can be found at: http://www.cantrip.org/traits.html
 

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,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top