나는 '안전한 bool 관용어'를 가리켰다. 그리고 무슨 일이 벌어지고 있는지 해독하려고 시도한 후에 (supplied on the site은 내가 의 이유를 이해할만큼 충분하지 않았기 때문에이 작동 함) 다음을 수행하기로 결정했다. 코드를 분리하고 가능한 한 간단하게하려고합니다. 아래의 사이트에서 제공되는 코드 :안전 bool idiom bool_type (및 안전 bool 관용구)은 어떻게 작동합니까?
class Testable {
bool ok_;
typedef void (Testable::*bool_type)() const;
void this_type_does_not_support_comparisons() const {}
public:
explicit Testable(bool b=true):ok_(b) {}
operator bool_type() const {
return ok_==true ?
&Testable::this_type_does_not_support_comparisons : 0;
}
};
나는이 그것을 중심으로 무엇을 것 같다 주어진 'bool_type'의 핵심 기반을 분석하기로 결정했다. 다음 줄을 감안할 때 : (인해 브라켓에, 아니 그렇게 쉽게)
typedef void (Testable::*bool_type)() const;
한 캔을이 bool_type 나타내는있는 '무효 시험 가능한 :: *'의 한 종류의 타입 정의의 추론. 이 추가로 다음과 같은 수정 및 기능을함으로써 입증 할 수는 호출
class Testable {
bool ok_;
typedef void (Testable::*bool_type)() const;
void this_type_does_not_support_comparisons() const {}
public:
explicit Testable(bool b=true):ok_(b) {}
bool_type Test; //Added this
operator bool_type() const {
return ok_==true ?
&Testable::this_type_does_not_support_comparisons : 0;
}
};
int main()
{
Testable Test;
int A = Test.Test; //Compiler will give a conversion error, telling us what type .Test is in the process
}
그것은 우리가 어떤 종류의 bool_type 것은 볼 수 있습니다 :
을 보여줍니다error: cannot convert 'void (Testable::*)()const' to 'int' in initialization
실제로 무효 '의 유형입니다 (시험 가능한 :: *) '.
이슈
는 여기 작물 :우리는 다음과 같은 기능을 수정하는 경우 :
operator bool_type() const {
return ok_==true ?
&Testable::this_type_does_not_support_comparisons : 0;
}
을 그리고로 돌려 :
그것은 다음과 같은 불만 생성operator void Testable::*() const //Same as bool_type, right?
{
return ok_==true ?
&Testable::this_type_does_not_support_comparisons : 0;
}
:
을error: expected identifier before '*' token
error: '< invalid operator >' declared as function returning a function
내 질문은 다음과 같습니다 :
'void (Testable :: *)가 실제로 bool_type의 typedef 일 경우 해당 불만 사항을 생성하는 이유는 무엇입니까?
및
여기에 무슨 일이 일어나고 있습니까? 이 정확하지
operator void Testable::*() const //Same as bool_type, right?
여기에 대해
왜 사람이 제목에 하나의 질문을 넣은 다음 텍스트에서 완전히 다른 질문을 하나합니까? * 매우 다른 두 가지 질문은 말할 것도없고? –
무엇이 문제입니까?'bool_type'은'void Testable :: some_function() const' 타입의 함수를 가리키는 멤버 함수 포인터입니다. "브라켓 팅 (bracketing)"으로 인한 혼란이 없습니다 (C++ 선언 문법이 정확하게 뷰티의 전형이 아니지만). –
나는 혼란이라는 말을 한 번도 안했는데, 나는 추론하기가 쉽지 않다고 (Testable :: * bool_type), 언뜻보기에 bool_type이라는 변수에 대한 포인터로 보일 것이다. typedef가 주어지면 사용 된 마지막 단어는 모든 것이 끝난 후에 typedef라는 것을 의미합니다. 괄호 안에 있지만 (선행에 반하는) – SSight3