bitset

Discussion in 'C++' started by Anders, Jul 17, 2003.

  1. Anders

    Anders Guest

    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;

    ...
    Anders, Jul 17, 2003
    #1
    1. Advertising

  2. "Anders" <anders> wrote in message
    news:3f170412$0$32530$...
    > 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
    John Harrison, Jul 17, 2003
    #2
    1. Advertising

  3. "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
    Victor Bazarov, Jul 17, 2003
    #3
  4. Anders

    Ron Natalie Guest

    "Anders" <anders> wrote in message news:3f170412$0$32530$...
    > 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.
    Ron Natalie, Jul 17, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo Nentwig

    Performance of java.util.BitSet

    Timo Nentwig, Dec 9, 2003, in forum: Java
    Replies:
    4
    Views:
    3,916
  2. Joshua
    Replies:
    4
    Views:
    384
    Esmond Pitt
    May 6, 2005
  3. Dill Hole
    Replies:
    2
    Views:
    434
    Mike Wahler
    Jul 5, 2003
  4. Victor Bazarov

    Re: bitset to UCHAR

    Victor Bazarov, Jul 8, 2003, in forum: C++
    Replies:
    1
    Views:
    547
    voger
    Jul 8, 2003
  5. Ninds
    Replies:
    14
    Views:
    725
    W Karas
    Dec 3, 2012
Loading...

Share This Page