bitset

A

Anders

Hello.

Can anyone tell me why you have to explicitly write the template parameters
in the .to_string() method? e.g:

bitset<8> bit(string("11"));
cout << bit.to_string<char, char_traits<char>, allocator<char> >() << endl;

...
 
J

John Harrison

Anders said:
Hello.

Can anyone tell me why you have to explicitly write the template parameters
in the .to_string() method? e.g:

bitset<8> bit(string("11"));
cout << bit.to_string<char, char_traits<char>, allocator<char> >() << endl;

Noramlly which version of a template function is called is worked out based
on the types of the parameters of that function. Since to_string does not
have any parameters you have to tell the compiler explicitly which kind of
string you want returned.

john
 
V

Victor Bazarov

Anders said:
Can anyone tell me why you have to explicitly write the template parameters
in the .to_string() method? e.g:

bitset<8> bit(string("11"));
cout << bit.to_string<char, char_traits<char>, allocator<char> >() <<
endl;

Because 'to_string' is a template member. You only need to specify
the first one, by the way: bit.to_string<char>()

However, *I* don't have to do that to output the actual bitset:

#include <bitset>
#include <iostream>
#include <string>

using namespace std;

int main()
{
bitset<8> bit(string("11"));
cout << bit << endl;
}

works fine for me. It prints
00000011

Victor
 
R

Ron Natalie

Anders said:
Hello.

Can anyone tell me why you have to explicitly write the template parameters
in the .to_string() method? e.g:

bitset<8> bit(string("11"));
cout << bit.to_string<char, char_traits<char>, allocator<char> >() << endl;

Functions are not overloaded by return value. It doesn't know what kind of
string you wanted to convert it to.
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top