printing bitset to_string().c_str() in template produces junk
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
gcc-7 (Ubuntu) |
New
|
Undecided
|
Unassigned |
Bug Description
I am trying to print a bitset with printf using a template, but I keep getting junk on the output.
I can print a bitset with printf(
but I thought it would be nice to do that with a template so I could just go:
printf(
template <const int mysize,class mytype> const char * mybitset(mytype myobj){ return std::bitset<
template <const int mysize> const char * mybitset2(int myobj){ return std::bitset<
template <const int mysize> const char * mybitset3(int myobj){ return std::bitset<
template <const int mysize> const char * mybitset4(int myobj){ return std::bitset<
template <const int mysize> std::string mybitset5(int myobj){ return std::bitset<
printf("testing bitset32 %s " ,std::bitset<
printf("testing bitset16 %s " ,std::bitset<
printf("testing mybitset32 %s " ,mybitset<
printf("testing mybitset8 %s " ,mybitset<
printf("testing mybitset2 16 %s " ,mybitset2<
printf("testing mybitset2 15 %s " ,mybitset2<
printf("testing mybitset2 8 %s " ,mybitset2<
printf("testing mybitset2 16 %s " ,mybitset2<
printf("testing mybitset3 15 %s " ,mybitset3<
printf("testing mybitset3 8 %s " ,mybitset3<
printf("testing mybitset3 16 %s \n",mybitset3<
printf("testing mybitset4 16 %s \n",mybitset4<
std::cout << "mybitset5" << mybitset5<
and the output I get is:
testing bitset32 000000000000000
testing mybitset4 16 `w�3V
mybitset5111100
So my template works fine up until I try to make it print a bitset of size 16. It works fine for a bitset of size 15 but not for 16.
I also tried this compiling with clang and it did the same thing, outputting garbage, working fine for a template with bitset size 15 and garbage for 16.
I took the code out of the bigger program and made another small program to show the bug:
======= ======= ======= ======= ======= ==
#include <stdio.h>
#include <cstdlib>
#include <bitset>
#include <strings.h>
#include <string.h>
#include <iostream>
template <const int mysize,class mytype> const char * mybitset(mytype myobj){ return std::bitset< mysize> (myobj) .to_string( ).c_str( ); } mysize> (myobj) .to_string( ).c_str( ); }
template <const int mysize> const char * mybitset2(int myobj){ return std::bitset<
#define bitsetvalue 0x255+0xf000
int main (int argc, char *argv[]) { :bitset< 32>(bitsetvalue ).to_string( ).c_str( )); :bitset< 16>(bitsetvalue ).to_string( ).c_str( )); :bitset< 15>(bitsetvalue ).to_string( ).c_str( )); 32,int> (bitsetvalue) ); 16,int> (bitsetvalue) ); 15,int> (bitsetvalue) ); 15>(bitsetvalue )); 32>(bitsetvalue ),strlen( mybitset< 32>(bitsetvalue ))); 32,int> (bitsetvalue) ,strlen( mybitset< 32,int> (bitsetvalue) )); 4,int>( bitsetvalue) ,strlen( mybitset< 4,int>( bitsetvalue) )); 8,int>( bitsetvalue) ,strlen( mybitset< 8,int>( bitsetvalue) )); 12,int> (bitsetvalue) ,strlen( mybitset< 12,int> (bitsetvalue) )); 15,int> (bitsetvalue) ,strlen( mybitset< 15,int> (bitsetvalue) )); 16,int> (bitsetvalue) ,strlen( mybitset< 16,int> (bitsetvalue) )); 16>(bitsetvalue )); 15>(bitsetvalue ));
printf("COMPILER VERSION %s\n",__VERSION__);
printf("bitset 32 %s \n",std:
printf("bitset 16 %s \n",std:
printf("bitset 15 %s \n",std:
printf("testing mybitset 32 %s \n",mybitset<
printf("testing mybitset 16 %s \n",mybitset<
printf("testing mybitset 15 %s \n",mybitset<
printf("testing mybitset 15 %s \n",mybitset<
printf("testing mybitset %s size %ld \n",mybitset<
printf("testing mybitset %s size %ld \n",mybitset<
printf("testing mybitset %s size %ld \n",mybitset<
printf("testing mybitset %s size %ld \n",mybitset<
printf("testing mybitset %s size %ld \n",mybitset<
printf("testing mybitset %s size %ld \n",mybitset<
printf("testing mybitset %s size %ld \n",mybitset<
printf("testing mybitset2 16 %s \n",mybitset2<
printf("testing mybitset2 15 %s \n",mybitset2<
}
=================
and the output gets:
$ g++ mybitsetsimple. cpp;./a. out 011110010010101 01
COMPILER VERSION 7.4.0
bitset 32 000000000000000
bitset 16 1111001001010101
bitset 15 111001001010101
testing mybitset 32
testing mybitset 16
testing mybitset 15 111001001010101
testing mybitset 15 111001001010101
testing mybitset size 0
testing mybitset size 0
testing mybitset 0101 size 4
testing mybitset 01010101 size 8
testing mybitset 001001010101 size 12
testing mybitset 111001001010101 size 15
testing mybitset size 0
testing mybitset2 16
testing mybitset2 15 111001001010101