bitset

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anders

    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<c har, char_traits<cha r>, allocator<char> >() << endl;

    ...


  • John Harrison

    #2
    Re: bitset


    "Anders" <anders> wrote in message
    news:3f170412$0 $32530$edfadb0f @dread16.news.t ele.dk...[color=blue]
    > Hello.
    >
    > Can anyone tell me why you have to explicitly write the template[/color]
    parameters[color=blue]
    > in the .to_string() method? e.g:
    >
    > bitset<8> bit(string("11" ));
    > cout << bit.to_string<c har, char_traits<cha r>, allocator<char> >() <<[/color]
    endl;[color=blue]
    >[/color]

    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


    Comment

    • Victor Bazarov

      #3
      Re: bitset

      "Anders" <anders> wrote...[color=blue]
      > Can anyone tell me why you have to explicitly write the template[/color]
      parameters[color=blue]
      > in the .to_string() method? e.g:
      >
      > bitset<8> bit(string("11" ));
      > cout << bit.to_string<c har, char_traits<cha r>, allocator<char> >() <<[/color]
      endl;

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

      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


      Comment

      • Ron Natalie

        #4
        Re: bitset


        "Anders" <anders> wrote in message news:3f170412$0 $32530$edfadb0f @dread16.news.t ele.dk...[color=blue]
        > 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<c har, char_traits<cha r>, allocator<char> >() << endl;[/color]

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



        Comment

        Working...