• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1062
  • Last Modified:

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!
0
shadow66
Asked:
shadow66
  • 7
  • 5
  • 3
1 Solution
 
nietodCommented:
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.
0
 
nietodCommented:
If that doesn't help you might try

 string s = bs.to_string<string>();
0
 
shadow66Author Commented:
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>()'.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nietodCommented:
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> >();
0
 
shadow66Author Commented:
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.
0
 
KangaRooCommented:
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);

0
 
nietodCommented:
Opps, I missed the last parameter.  Thanks Kangaroo.
0
 
KangaRooCommented:
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!
}
0
 
KangaRooCommented:
I wonder which compilers actually support this stuff?
0
 
nietodCommented:
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.
0
 
KangaRooCommented:
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 `('
0
 
nietodCommented:
>> 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....
0
 
KangaRooCommented:
I see, thanx for the note on this Q; I have never used bitset<> before, nor  this method of invoking template members. Learned some thing new again :)

For everyones information, a brief summary of the new stuff in C++ since the ARM see http://www.ocsltd.com/c++/ and in particular http://www.ocsltd.com/c++/cppptdis.html
0
 
shadow66Author Commented:
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!
0
 
nietodCommented:
>> 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.)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now