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(); } template <const int mysize> const char * mybitset2(int myobj){ return std::bitset<mysize>(myobj).to_string().c_str(); } #define bitsetvalue 0x255+0xf000 int main (int argc, char *argv[]) { printf("COMPILER VERSION %s\n",__VERSION__); printf("bitset 32 %s \n",std::bitset<32>(bitsetvalue).to_string().c_str()); printf("bitset 16 %s \n",std::bitset<16>(bitsetvalue).to_string().c_str()); printf("bitset 15 %s \n",std::bitset<15>(bitsetvalue).to_string().c_str()); printf("testing mybitset 32 %s \n",mybitset<32,int>(bitsetvalue)); printf("testing mybitset 16 %s \n",mybitset<16,int>(bitsetvalue)); printf("testing mybitset 15 %s \n",mybitset<15,int>(bitsetvalue)); printf("testing mybitset 15 %s \n",mybitset<15>(bitsetvalue)); printf("testing mybitset %s size %ld \n",mybitset<32>(bitsetvalue),strlen(mybitset<32>(bitsetvalue))); printf("testing mybitset %s size %ld \n",mybitset<32,int>(bitsetvalue),strlen(mybitset<32,int>(bitsetvalue))); printf("testing mybitset %s size %ld \n",mybitset<4,int>(bitsetvalue),strlen(mybitset<4,int>(bitsetvalue))); printf("testing mybitset %s size %ld \n",mybitset<8,int>(bitsetvalue),strlen(mybitset<8,int>(bitsetvalue))); printf("testing mybitset %s size %ld \n",mybitset<12,int>(bitsetvalue),strlen(mybitset<12,int>(bitsetvalue))); printf("testing mybitset %s size %ld \n",mybitset<15,int>(bitsetvalue),strlen(mybitset<15,int>(bitsetvalue))); printf("testing mybitset %s size %ld \n",mybitset<16,int>(bitsetvalue),strlen(mybitset<16,int>(bitsetvalue))); printf("testing mybitset2 16 %s \n",mybitset2<16>(bitsetvalue)); printf("testing mybitset2 15 %s \n",mybitset2<15>(bitsetvalue)); } ================= and the output gets: $ g++ mybitsetsimple.cpp;./a.out COMPILER VERSION 7.4.0 bitset 32 00000000000000001111001001010101 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 -- You received this bug notification because you are a member of Ubuntu Bugs, which is subscribed to Ubuntu. https://bugs.launchpad.net/bugs/1829415 Title: printing bitset to_string().c_str() in template produces junk To manage notifications about this bug go to: https://bugs.launchpad.net/ubuntu/+source/gcc-7/+bug/1829415/+subscriptions -- ubuntu-bugs mailing list ubuntu-bugs@lists.ubuntu.com https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs