Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   bitset (http://www.velocityreviews.com/forums/t275747-bitset.html)

Anders 07-17-2003 08:16 PM

bitset
 
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;

...



John Harrison 07-17-2003 08:24 PM

Re: bitset
 

"Anders" <anders> wrote in message
news:3f170412$0$32530$edfadb0f@dread16.news.tele.d k...
> 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



Victor Bazarov 07-17-2003 08:27 PM

Re: bitset
 
"Anders" <anders> wrote...
> 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



Ron Natalie 07-17-2003 08:31 PM

Re: bitset
 

"Anders" <anders> wrote in message news:3f170412$0$32530$edfadb0f@dread16.news.tele.d k...
> 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.





All times are GMT. The time now is 11:42 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.