Solved

Using bitset.to_string()

Posted on 2000-02-27
15
1,041 Views
Last Modified: 2008-03-17
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
Comment
Question by:shadow66
  • 7
  • 5
  • 3
15 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2563430
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
 
LVL 22

Expert Comment

by:nietod
ID: 2563434
If that doesn't help you might try

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

Author Comment

by:shadow66
ID: 2563472
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
Industry Leaders: 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!

 
LVL 22

Expert Comment

by:nietod
ID: 2563492
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
 

Author Comment

by:shadow66
ID: 2563654
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2565069
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
 
LVL 22

Expert Comment

by:nietod
ID: 2565084
Opps, I missed the last parameter.  Thanks Kangaroo.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2565123
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2565129
I wonder which compilers actually support this stuff?
0
 
LVL 22

Expert Comment

by:nietod
ID: 2565172
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2565330
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
 
LVL 22

Expert Comment

by:nietod
ID: 2565383
>> 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
 
LVL 7

Accepted Solution

by:
KangaRoo earned 50 total points
ID: 2565486
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
 

Author Comment

by:shadow66
ID: 2567564
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
 
LVL 22

Expert Comment

by:nietod
ID: 2568385
>> 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question