安全bool成语 - 保护功能无法访问 [英] Safe bool idiom - protected function not accessible

查看:82
本文介绍了安全bool成语 - 保护功能无法访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于安全bool习语的文章

http://www.artima.com/cppsource/safeboolP.html) ,Bjorn Karlsson给出

以下代码(稍加修改):

class safe_bool_base {

protected:

typedef void(safe_bool_base :: * bool_type)()const;

void this_type_does_not_support_comparisons( )const {}

safe_bool_base(){}

safe_bool_base(const safe_bool_base&){}

safe_bool_base& operator =(const safe_bool_base&){return * this;}

~safe_bool_base(){}

};


模板< typename T = voidclass safe_bool:public safe_bool_base {

public:

operator bool_type()const {

return(static_cast< const T * >(this)) - > boolean_test()

? & safe_bool_base :: this_type_does_not_support_compar isons:0;

}

受保护:

~safe_bool(){}

};

模板< class safe_bool< void:public safe_bool_base {

public:

operator bool_type()const {

return boolean_test()== true?

& safe_bool_base :: this_type_does_not_support_compar isons //错误!

:0;

}

受保护:

虚拟bool boolean_test()const = 0;

virtual~safe_bool(){}

};

模板< typename T,typename U>

void operator ==(const safe_bool< T>& lhs,const safe_bool< U>& rhs) {

lhs.this_type_does_not_support_comparisons();

返回;

}


模板< typename T,typename U>

void operator!=(const safe_bool< T>& lhs,const safe_bool< U>& rhs){

lhs.this_type_does_not_support_comparisons() ;

返回;

}


类Testable_with_virtual:public safe_bool< {

protected:

bool boolean_test()const {

返回false;

}

};


class Testable_without_virtual:

public safe_bool< Testable_without_virtual {

public:

bool boolean_test()const {

return true;

}

};


这不能用VC8,VC7或Comeau编译,但它与EDG一起编译,

GNU和VC6(稍微修改为

使用void template args的非conformancies)。错误发生在由错误!指示的

行上。在Comeau上写道:


" ComeauTest.c",第26行:错误:受保护的功能

" safe_bool_base :: this_type_does_not_support_compar isons" (在

第4行声明)无法通过safe_bool_base进行访问。指针或对象

& safe_bool_base :: this_type_does_not_support_compar isons //错误!

^


请注意,只有类模板'专业化有问题。这是否像往常一样?b $ b好​​吧?


干杯! --M

In an article on the safe bool idiom
(http://www.artima.com/cppsource/safeboolP.html), Bjorn Karlsson gives
the following code (slightly modified):

class safe_bool_base {
protected:
typedef void (safe_bool_base::*bool_type)() const;
void this_type_does_not_support_comparisons() const {}

safe_bool_base() {}
safe_bool_base(const safe_bool_base&) {}
safe_bool_base& operator=(const safe_bool_base&) {return *this;}
~safe_bool_base() {}
};

template <typename T=voidclass safe_bool : public safe_bool_base {
public:
operator bool_type() const {
return (static_cast<const T*>(this))->boolean_test()
? &safe_bool_base::this_type_does_not_support_compar isons : 0;
}
protected:
~safe_bool() {}
};
template<class safe_bool<void: public safe_bool_base {
public:
operator bool_type() const {
return boolean_test()==true ?
&safe_bool_base::this_type_does_not_support_compar isons //Error!
: 0;
}
protected:
virtual bool boolean_test() const=0;
virtual ~safe_bool() {}
};
template <typename T, typename U>
void operator==(const safe_bool<T>& lhs,const safe_bool<U>& rhs) {
lhs.this_type_does_not_support_comparisons();
return;
}

template <typename T,typename U>
void operator!=(const safe_bool<T>& lhs,const safe_bool<U>& rhs) {
lhs.this_type_does_not_support_comparisons();
return;
}

class Testable_with_virtual : public safe_bool<{
protected:
bool boolean_test() const {
return false;
}
};

class Testable_without_virtual :
public safe_bool <Testable_without_virtual{
public:
bool boolean_test() const {
return true;
}
};

This does not compile with VC8, VC7, or Comeau, but it does with EDG,
GNU, and VC6 (slightly modified further to account for
non-conformancies with void template args). The error occurs on the
line indicated by "Error!" and on Comeau reads:

"ComeauTest.c", line 26: error: protected function
"safe_bool_base::this_type_does_not_support_compar isons" (declared at
line 4) is not accessible through a "safe_bool_base" pointer or object
&safe_bool_base::this_type_does_not_support_compar isons //Error!
^

Notice that only the class template''s specialization has a problem. Is
Comeau right as usual?

Cheers! --M

推荐答案

mlimber写道:
mlimber wrote:

在一篇文章中关于安全bool成语

http:/ /www.artima.com/cppsource/safeboolP.html),Bjorn Karlsson给出了

以下代码(稍加修改):


class safe_bool_base {

protected:

typedef void(safe_bool_base :: * bool_type)()const;

void this_type_does_not_support_comparisons()const {}


safe_bool_base(){}

safe_bool_base(const safe_bool_base&){}

safe_bool_base& operator =(const safe_bool_base&){return * this;}

~safe_bool_base(){}

};


模板< typename T = voidclass safe_bool:public safe_bool_base {

public:

operator bool_type()const {

return(static_cast< const T * >(this)) - > boolean_test()

? & safe_bool_base :: this_type_does_not_support_compar isons:0;

}

受保护:

~safe_bool(){}

};

模板< class safe_bool< void:public safe_bool_base {

public:

operator bool_type()const {

return boolean_test()== true?

& safe_bool_base :: this_type_does_not_support_compar isons //错误!

:0;

}

受保护:

虚拟bool boolean_test()const = 0;

virtual~safe_bool(){}

};
In an article on the safe bool idiom
(http://www.artima.com/cppsource/safeboolP.html), Bjorn Karlsson gives
the following code (slightly modified):

class safe_bool_base {
protected:
typedef void (safe_bool_base::*bool_type)() const;
void this_type_does_not_support_comparisons() const {}

safe_bool_base() {}
safe_bool_base(const safe_bool_base&) {}
safe_bool_base& operator=(const safe_bool_base&) {return *this;}
~safe_bool_base() {}
};

template <typename T=voidclass safe_bool : public safe_bool_base {
public:
operator bool_type() const {
return (static_cast<const T*>(this))->boolean_test()
? &safe_bool_base::this_type_does_not_support_compar isons : 0;
}
protected:
~safe_bool() {}
};
template<class safe_bool<void: public safe_bool_base {
public:
operator bool_type() const {
return boolean_test()==true ?
&safe_bool_base::this_type_does_not_support_compar isons //Error!
: 0;
}
protected:
virtual bool boolean_test() const=0;
virtual ~safe_bool() {}
};



将错误行改为:


return boolean_test()== true?

& safe_bool :: this_type_does_not_support_comparis ons

:0; //好吧


并且错误应该修复。


Greg

Change the line with the error to:

return boolean_test()==true ?
&safe_bool::this_type_does_not_support_comparis ons
: 0; // OK

and the error should be fixed.

Greg

Greg写道:
Greg wrote:

mlimber写道:
mlimber wrote:

在一篇关于安全bool成语的文章中

http://www.artima。 com / cppsource / safeboolP.html),Bjorn Karlsson给出了

以下代码(稍加修改):


class safe_bool_base {

受保护:

typedef void(safe_bool_base :: * bool_type)()const;

void this_type_does_not_support_comparisons()const {}

safe_bool_base(){}

safe_bool_base(const safe_bool_base&){}

safe_bool_base& operator =(const safe_bool_base&){return * this;}

~safe_bool_base(){}

};


模板< typename T = voidclass safe_bool:public safe_bool_base {

public:

operator bool_type()const {

return(static_cast< const T * >(this)) - > boolean_test()

? & safe_bool_base :: this_type_does_not_support_compar isons:0;

}

受保护:

~safe_bool(){}

};

模板< class safe_bool< void:public safe_bool_base {

public:

operator bool_type()const {

return boolean_test()== true?

& safe_bool_base :: this_type_does_not_support_compar isons //错误!

:0;

}

受保护:

虚拟bool boolean_test()const = 0;

virtual~safe_bool(){}

};
In an article on the safe bool idiom
(http://www.artima.com/cppsource/safeboolP.html), Bjorn Karlsson gives
the following code (slightly modified):

class safe_bool_base {
protected:
typedef void (safe_bool_base::*bool_type)() const;
void this_type_does_not_support_comparisons() const {}

safe_bool_base() {}
safe_bool_base(const safe_bool_base&) {}
safe_bool_base& operator=(const safe_bool_base&) {return *this;}
~safe_bool_base() {}
};

template <typename T=voidclass safe_bool : public safe_bool_base {
public:
operator bool_type() const {
return (static_cast<const T*>(this))->boolean_test()
? &safe_bool_base::this_type_does_not_support_compar isons : 0;
}
protected:
~safe_bool() {}
};
template<class safe_bool<void: public safe_bool_base {
public:
operator bool_type() const {
return boolean_test()==true ?
&safe_bool_base::this_type_does_not_support_compar isons //Error!
: 0;
}
protected:
virtual bool boolean_test() const=0;
virtual ~safe_bool() {}
};



将错误行改为:


return boolean_test()== true?

& safe_bool :: this_type_does_not_support_comparis ons

:0; //好的


,错误应该修复。


Change the line with the error to:

return boolean_test()==true ?
&safe_bool::this_type_does_not_support_comparis ons
: 0; // OK

and the error should be fixed.



的确如此。现在的问题是,为什么正常的模板类可以获得该成员但是明确的专业化不能?


干杯! --M

Indeed. Now the question is, Why could the normal template class get to
that member but the explicit specialization could not?

Cheers! --M


mlimber写道:
mlimber wrote:

Greg写道:
Greg wrote:


将错误行改为:


return boolean_test()== true?

& safe_bool :: this_type_does_not_support_comparis ons

:0; //好的


,错误应该修复。

Change the line with the error to:

return boolean_test()==true ?
&safe_bool::this_type_does_not_support_comparis ons
: 0; // OK

and the error should be fixed.



的确如此。现在的问题是,为什么正常的模板类可以获得该成员但是明确的专业化不能?


Indeed. Now the question is, Why could the normal template class get to
that member but the explicit specialization could not?



两人都无法访问:


int main()

{

safe_bool< intsb;

}


错误:''safe_bool< T> ::〜safe_bool()[with T = int]' '受到保护


格雷格

Neither one has access:

int main()
{
safe_bool<intsb;
}

error: ''safe_bool<T>::~safe_bool() [with T = int]'' is protected

Greg


这篇关于安全bool成语 - 保护功能无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆