2016-09-22 2 views
2

std::runtime_error:runtime_error(const string& arg)에 대한 자체 정의가 필요합니다. 우리는 같은, 즉, 다른 생성자의 관점에서 std::runtime_error:runtime_error(const char*)을 같은 생성자를 구현 : 생성자 std::runtime_error::runtime_error(const string& arg)이 존재하지 않기 때문에 GCC-4.9.1와gcc 4.9.1 표준을 준수하지 않습니까? (std :: runtime_error)

namespace std { 
    runtime_error::runtime_error(const string& arg) 
    : runtime_error(arg.c_str()) { 
    ... 
    } 
} 

, 이것은 불가능하다. GCC에서/4.9.1// C++/4.9.1/stdexcept에게을 포함, 우리는 다음을 참조하십시오

... 
class runtime_error : public exception 
{ 
    string _M_msg; 
    public: 
    /** Takes a character string describing the error. */ 
    explicit 
    runtime_error(const string& __arg); 
    virtual ~runtime_error() _GLIBCXX_USE_NOEXCEPT; 
    /** Returns a C-style character string describing the general cause of 
    * the current error (the same string passed to the ctor). */ 
    virtual const char* 
    what() const _GLIBCXX_USE_NOEXCEPT; 
}; 
... 

기준은 명확하게 명시 runtime_error(const char*) 생성자가되어야한다고 말한다.

19.2.6 클래스 runtime_error는 [runtime.error]

namespace std { 
class runtime_error : public exception { 
public: 
explicit runtime_error(const string& what_arg); 
explicit runtime_error(const char* what_arg); 
}; 
+5

"* 우리는 다른 생성자에 대해 이러한 생성자를 구현합니다. *"왜 그러고 싶니? 'std :: string'을 취하는 것은 길이를 가지고 있고, 다른 것은 길이를 계산하기 위해'strlen'을해야합니다. 그것은 단지 시간 낭비 일뿐입니다. 또한 표준 라이브러리에서 클래스를 다시 작성할 수 없습니다. –

+0

'-std = C++ 11'을 사용하고 있습니까? –

+0

@FredLarson 기본값이어야합니다. –

답변

1

는 아마도 이것은 원래의 질문에 대답하지 않지만 의도는 정말 runtime_error 인스턴스를 차단하는 경우, 당신은이 (asuming GCC처럼 할 수) 사용됩니다

namespace std { 
    runtime_error::runtime_error(const string& arg) 
    #if (__GNUC__ > 4) 
    : runtime_error(arg.c_str()) 
    #else 
    : _M_msg(arg) 
    #endif 
    { 
    // intercept here! 
    } 
} 

희망, 호출 된 runtime_error(const char*)의 모든 희망과 욕망을 끊을 runtime_error(const string&)의 측면에서 미래에 구현되지 않습니다. =)

관련 문제