1

have this problem. It seem to me that compiler work badly when in case 3 shown in code bellow silently accepts conversion from class String to const char*

I think compile time error should be shown. Am missing something or is this really a GCC compiler bug?

Compiler is GCC (TDM-1) 4.9.2

Example code main.cpp is compiled with command line: g++ main.cpp

main.cpp:

#include <string>
#include <stdio.h>

class LogBlock {
public:
    explicit LogBlock( const char* info )
    :   m_info( info )
    {
        printf( "%s {\n", info );
    }

    ~LogBlock()
    {
        printf( "} %s done\n", m_info.c_str() );
    }

private:
    std::string m_info;
};


class String {
public:
    explicit String( const char* s ) : m_str( s ) {}

    const char* m_str;
};


int main()
{
    const char* bufInfo = "main";

    //// case 1) This behaves as expected - compiles and executes without problem:
    //LogBlock dummy_name1( "main" );

    //// case 2) This behaves as expected - compiler shows:
    //// "error: no matching function for call to 'LogBlock::LogBlock(String)"
    //LogBlock dummy_name2( String( "main" ) );

    // case 3) This does not behave as expected - compiles without any error message and works incorectly.
    // I think compiler should show same arror as in case 2) Why it does not work that?
    LogBlock dummy_name3( String( bufInfo ) );

    printf( "main body\n" );

    return 0;
}
user3123061
  • 757
  • 5
  • 14

0 Answers0