Link to home
Start Free TrialLog in
Avatar of shadow66
shadow66

asked on

Using bitset.to_string()

How do I use the to_string() method of a bitset? This code fails:

<bitset.h>
<string.h>
int main() {
  bitset<32> bs;
  string s = bs.to_string();        // Compiler Error (template-related error)
  return 0;
}

I know the to_string method is some kind of a template, but none of my references give examples on how to use it, just (ugly) syntax.

What would make this example work?
Thanks!
Avatar of nietod
nietod

That works for me on VC 6.  However, I did make 1 or 2 small changes.

If your compiler has them, you should be using <bitset> not <bitset.h> and <string> not <string.h>.  You can get weird problems with the .h versions which are outdated.  When you use the non.h versions, though the classes are all defined in the "std" namespace, so you must use that namespace with a

using namespace std;

or you must specify "std::" before the class names.
If that doesn't help you might try

 string s = bs.to_string<string>();
Avatar of shadow66

ASKER

No change. Here's the whole thing:

#include <condefs.h>
#include <bitset>
#include <string>
#pragma hdrstop
using namespace std;
int main()
{
    bitset<32> bs;
    string s = bs.to_string();
    return 0;
}

Attempt to compile produces:
[C++ Error] experiment.cpp(9): E2285 Could not find a match for 'bitset<32>::to_string<charT,traits,Allocator>()'.
***************************************************************
changing last line to:
  string s = bs.to_string<string>();
results in:
[C++ Error] experiment.cpp(9): E2299 Cannot generate template specialization from 'bitset<32>::to_string<charT,traits,Allocator>() const'.
[C++ Error] experiment.cpp(9): E2285 Could not find a match for 'bitset<32>::to_string<charT,traits,Allocator>()'.

It works in VC because VC doesn't really support member templates yet so this is not a template member function, it always returns string--sot it is simpler.  But because of this I don't have the "propper" definition for the function, like you do.  You might try

string s = bs.to_string<char>();

or maybe

string s = bs.to_string<char,char_traits<char> >();
I appreciate your continuing efforts.

Both
  string s = bs.to_string<char>();
and
  string s = bs.to_string<char,char_traits<char> >();

had the same results as
  string s = bs.to_string<string>();


Online help shows the syntax of to_string() as:

template<class charT, class traits, class Allocator>
basic_string<charT, traits, Allocator>
to_string();

Typically, there's nothing resembling an example of using to_string() anywhere else in the "help" system. Nor is Stroustrop's C++ Programming explanation any more helpful.
You need to invok ethis template member using explicit parameter specifications

#include <bitset>
#include <string>

using namespace std;
int main()
{
    bitset<32> bs;
    string s = bs.template to_string<char, char_traits<char>, allocator<char> >();
    return 0;
}

I can imagine you don't like this, a utility function couldl help a bit:

template<typename CharT, typename Traits, typename Alloc, typename BitSet>
inline void
get_bitset_as_string(basic_string<CharT, Traits, Alloc>& s, const BitSet& bs)
{
     s = bs.template to_string<CharT, Traits, Alloc>();
}

template<typename String, typename BitSet>
inline String
bitset_to_string(const BitSet& bs)
{
     String tmp;
     get_bitset_as_string(tmp, bs);
     return tmp;
}


which can be used as:

    string g = bitset_to_string<string, bitset<32> >(bs);

Opps, I missed the last parameter.  Thanks Kangaroo.
Also, note the remarkable syntax:

struct Foo
{
    template<typename T> void bar();
};

f()
{
   Foo foo;
   foo.template bar<int>(); // Note the keyword 'template' behind the member-dot!
}
I wonder which compilers actually support this stuff?
I missed that "template".  wonderful stuff :-)

I thought that gcc was very close to the standard and I thought that was one that you used.
Yes, the version I use, gcc/egcs 2.95.2, actually does compile this (but also without that template keyword in the call...) code. I was unclear about that in my comment. Sadly the standard library that comes with GCC is not so compliant with the standard and I had to use SGI's STL (freely available from their site http://www.sgi.com/Technology/STL/download.html ) to get it working.

I doubt however wether BC or VC will compile this, and the message shadow66 showed does not look like it is from gcc, since that would be something like:   test.cpp:9: parse error before `('
>> I doubt however wether BC or VC will compile this
I don't doubt it at all.  I know it won't.  VC uses a non-template form that allways returns a "string" and BC probably does the same.  

>> the message shadow66 showed does
>> not look like it is from gcc
I didn't know what he was using, just that it really did support (to some extent) member template functions so I figured you would have a better chance of helping him than me.  I just had to guess at stuff, you try compiles.  Though if either of the compilers don't behave perfectly....
ASKER CERTIFIED SOLUTION
Avatar of KangaRoo
KangaRoo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well, it looks like this posting has taken a life of its own. I'm glad I checked it tonight--wouldn't want to leave you hanging.

string s = bs.template to_string<char, char_traits<char>, allocator<char> >();

works like a champ. I saw that in Stroustrop's book, but I thought, "that's gotta be some kind of syntax definition, not actual code." Yikes! KangaRoo's suggested utility is *very* appreciated!

Thanks to nietod for bringing in KangaRoo. That's a level of service I wasn't expecting from EE!

Sorry for not telling you sooner that I'm using Borland C++ Builder 4.0. Didn't mean to withhold that info and hope it didn't make resolution more difficult.

As for equitable payment, how about this: I'll give my 50 points to KangaRoo for the answer and then post another "shell question" for nietod for a 25 point "finder's fee".

I really appreciate your efforts. As a workaround, I had written a short function to do the conversion manually, but I really did want to know how to use the bitset member (template) function. I'm hoping it's faster!
>> I'm using Borland C++ Builder 4.0.
Wish I had known.  That I actually have.  I didn't realize that they supported templates so well.  (I knew they were better than VC's, but I didn't know they were that good.  BCB 3 was actually very poor in this area.)