473,729 Members | 2,340 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Safe bool idiom - protected function not accessible

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_com parisons() 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<co nst T*>(this))->boolean_test ()
? &safe_bool_base ::this_type_doe s_not_support_c omparisons : 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_doe s_not_support_c omparisons //Error!
: 0;
}
protected:
virtual bool boolean_test() const=0;
virtual ~safe_bool() {}
};
template <typename T, typename U>
void operator==(cons t safe_bool<T>& lhs,const safe_bool<U>& rhs) {
lhs.this_type_d oes_not_support _comparisons();
return;
}

template <typename T,typename U>
void operator!=(cons t safe_bool<T>& lhs,const safe_bool<U>& rhs) {
lhs.this_type_d oes_not_support _comparisons();
return;
}

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

class Testable_withou t_virtual :
public safe_bool <Testable_witho ut_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_doe s_not_support_c omparisons" (declared at
line 4) is not accessible through a "safe_bool_base " pointer or object
&safe_bool_base ::this_type_doe s_not_support_c omparisons //Error!
^

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

Cheers! --M

Dec 14 '06 #1
14 2472
mlimber wrote:
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_com parisons() 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<co nst T*>(this))->boolean_test ()
? &safe_bool_base ::this_type_doe s_not_support_c omparisons : 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_doe s_not_support_c omparisons //Error!
: 0;
}
protected:
virtual bool boolean_test() const=0;
virtual ~safe_bool() {}
};
Change the line with the error to:

return boolean_test()= =true ?
&safe_bool::thi s_type_does_not _support_compar isons
: 0; // OK

and the error should be fixed.

Greg

Dec 14 '06 #2
Greg wrote:
mlimber wrote:
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_com parisons() 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<co nst T*>(this))->boolean_test ()
? &safe_bool_base ::this_type_doe s_not_support_c omparisons : 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_doe s_not_support_c omparisons //Error!
: 0;
}
protected:
virtual bool boolean_test() const=0;
virtual ~safe_bool() {}
};

Change the line with the error to:

return boolean_test()= =true ?
&safe_bool::thi s_type_does_not _support_compar isons
: 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?

Cheers! --M

Dec 14 '06 #3
mlimber wrote:
Greg wrote:

Change the line with the error to:

return boolean_test()= =true ?
&safe_bool::thi s_type_does_not _support_compar isons
: 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?
Neither one has access:

int main()
{
safe_bool<intsb ;
}

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

Greg

Dec 14 '06 #4
Greg wrote:
mlimber wrote:
Greg wrote:
>
Change the line with the error to:
>
return boolean_test()= =true ?
&safe_bool::thi s_type_does_not _support_compar isons
: 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?

Neither one has access:

int main()
{
safe_bool<intsb ;
}

error: 'safe_bool<T>:: ~safe_bool() [with T = int]' is protected
The destructor is protected, indicating that safe_bool should only be
used as a base class. The derived classes should have full access to
the protected members of the base class, but it seems that only the
non-specialized derived class does. Any idea why (or is this one of the
rare bugs in Comeau)?

Cheers! --M

Dec 15 '06 #5

mlimber wrote:
Greg wrote:
mlimber wrote:
Greg wrote:

Change the line with the error to:

return boolean_test()= =true ?
&safe_bool::thi s_type_does_not _support_compar isons
: 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?
Neither one has access:

int main()
{
safe_bool<intsb ;
}

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

The destructor is protected, indicating that safe_bool should only be
used as a base class. The derived classes should have full access to
the protected members of the base class, but it seems that only the
non-specialized derived class does. Any idea why (or is this one of the
rare bugs in Comeau)?
No, Comeau is correct: a derived class does not have access to
protected members of a base class - either for the purposes of
obtaining a member pointer to one or even to use the protected member
in any kind of object expression. The only protected members that a
class has that kind of access are its own protected members and the
protected members of classes that derive from it. And that rule is in
fact the reason why changing the class of the member pointer from
safe_bool_base to safe_bool eliminates the access violation.

Therefore every member pointer above that refers to a protected member
of a base class - is an access violation waiting to happen. The actual
error is deferred of course until the code in question is instantiated.
So there is nothing about the void specialization that is any different
than the others templates - they all have the same problem.

Greg

Dec 15 '06 #6
Greg wrote:
mlimber wrote:
Greg wrote:
mlimber wrote:
Greg wrote:
>
Change the line with the error to:
>
return boolean_test()= =true ?
&safe_bool::thi s_type_does_not _support_compar isons
: 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?
>
Neither one has access:
>
int main()
{
safe_bool<intsb ;
}
>
error: 'safe_bool<T>:: ~safe_bool() [with T = int]' is protected
The destructor is protected, indicating that safe_bool should only be
used as a base class. The derived classes should have full access to
the protected members of the base class, but it seems that only the
non-specialized derived class does. Any idea why (or is this one of the
rare bugs in Comeau)?

No, Comeau is correct: a derived class does not have access to
protected members of a base class - either for the purposes of
obtaining a member pointer to one or even to use the protected member
in any kind of object expression.
Can you cite the standard in this regard?
The only protected members that a
class has that kind of access are its own protected members and the
protected members of classes that derive from it.
Huh? A class can obtain a pointer to a member of a class that derives
from it? Unless you're talking only about virtual functions, you've
lost me completely.
And that rule is in
fact the reason why changing the class of the member pointer from
safe_bool_base to safe_bool eliminates the access violation.

Therefore every member pointer above that refers to a protected member
of a base class - is an access violation waiting to happen. The actual
error is deferred of course until the code in question is instantiated.
So there is nothing about the void specialization that is any different
than the others templates - they all have the same problem.
But the code given in my OP *does* instantiate both the specialized and
non-specialized versions, but only the specialized version causes the
compiler to choke.

Cheers! --M

Dec 15 '06 #7
Greg wrote:
mlimber wrote:
Greg wrote:
mlimber wrote:
Greg wrote:
>
Change the line with the error to:
>
return boolean_test()= =true ?
&safe_bool::thi s_type_does_not _support_compar isons
: 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?
>
Neither one has access:
>
int main()
{
safe_bool<intsb ;
}
>
error: 'safe_bool<T>:: ~safe_bool() [with T = int]' is protected
The destructor is protected, indicating that safe_bool should only be
used as a base class. The derived classes should have full access to
the protected members of the base class, but it seems that only the
non-specialized derived class does. Any idea why (or is this one of the
rare bugs in Comeau)?

No, Comeau is correct: a derived class does not have access to
protected members of a base class - either for the purposes of
obtaining a member pointer to one or even to use the protected member
in any kind of object expression.
Can you cite the standard in this regard?
The only protected members that a
class has that kind of access are its own protected members and the
protected members of classes that derive from it.
Huh? A class can obtain a pointer to a member of a class that derives
from it? Unless you're talking only about virtual functions, you've
lost me completely.
And that rule is in
fact the reason why changing the class of the member pointer from
safe_bool_base to safe_bool eliminates the access violation.

Therefore every member pointer above that refers to a protected member
of a base class - is an access violation waiting to happen. The actual
error is deferred of course until the code in question is instantiated.
So there is nothing about the void specialization that is any different
than the others templates - they all have the same problem.
But the code given in my OP *does* instantiate both the specialized and
non-specialized versions, but only the specialized version causes the
compiler to choke.

Cheers! --M

Dec 15 '06 #8
Greg wrote:
mlimber wrote:
Greg wrote:
mlimber wrote:
Greg wrote:
>
Change the line with the error to:
>
return boolean_test()= =true ?
&safe_bool::thi s_type_does_not _support_compar isons
: 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?
>
Neither one has access:
>
int main()
{
safe_bool<intsb ;
}
>
error: 'safe_bool<T>:: ~safe_bool() [with T = int]' is protected
The destructor is protected, indicating that safe_bool should only be
used as a base class. The derived classes should have full access to
the protected members of the base class, but it seems that only the
non-specialized derived class does. Any idea why (or is this one of the
rare bugs in Comeau)?

No, Comeau is correct: a derived class does not have access to
protected members of a base class - either for the purposes of
obtaining a member pointer to one or even to use the protected member
in any kind of object expression.
Can you cite the standard in this regard?
The only protected members that a
class has that kind of access are its own protected members and the
protected members of classes that derive from it.
Huh? A class can obtain a pointer to a member of a class that derives
from it? Unless you're talking only about virtual functions, you've
lost me completely.
And that rule is in
fact the reason why changing the class of the member pointer from
safe_bool_base to safe_bool eliminates the access violation.

Therefore every member pointer above that refers to a protected member
of a base class - is an access violation waiting to happen. The actual
error is deferred of course until the code in question is instantiated.
So there is nothing about the void specialization that is any different
than the others templates - they all have the same problem.
But the code given in my OP *does* instantiate both the specialized and
non-specialized versions, but only the specialized version causes the
compiler to choke.

Cheers! --M

Dec 15 '06 #9
Greg wrote:
mlimber wrote:
Greg wrote:
mlimber wrote:
Greg wrote:
>
Change the line with the error to:
>
return boolean_test()= =true ?
&safe_bool::thi s_type_does_not _support_compar isons
: 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?
>
Neither one has access:
>
int main()
{
safe_bool<intsb ;
}
>
error: 'safe_bool<T>:: ~safe_bool() [with T = int]' is protected
The destructor is protected, indicating that safe_bool should only be
used as a base class. The derived classes should have full access to
the protected members of the base class, but it seems that only the
non-specialized derived class does. Any idea why (or is this one of the
rare bugs in Comeau)?

No, Comeau is correct: a derived class does not have access to
protected members of a base class - either for the purposes of
obtaining a member pointer to one or even to use the protected member
in any kind of object expression.
Can you cite the standard in this regard?
The only protected members that a
class has that kind of access are its own protected members and the
protected members of classes that derive from it.
Huh? A class can obtain a pointer to a member of a class that derives
from it? Unless you're talking only about virtual functions, you've
lost me completely.
And that rule is in
fact the reason why changing the class of the member pointer from
safe_bool_base to safe_bool eliminates the access violation.

Therefore every member pointer above that refers to a protected member
of a base class - is an access violation waiting to happen. The actual
error is deferred of course until the code in question is instantiated.
So there is nothing about the void specialization that is any different
than the others templates - they all have the same problem.
But the code given in my OP *does* instantiate both the specialized and
non-specialized versions, but only the specialized version causes the
compiler to choke.

Cheers! --M

Dec 15 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2323
by: Mikhail N. Kupchik | last post by:
Hi All. I have a question regarding Herb Sutter's idiom of implementation of operator= via nonthrowing swap() member function, to guarantee strict exception safety. The idea of the idiom is shown by the example: -- code fragment 1 ---------------------------------------------------- class MyClass
7
2802
by: Gary | last post by:
I would like to make a strongly typed, sortable collection by leveraging a Framework class. I began by looking at CollectionBase but it doesn't have built-int sorting. I would prefer to derive from ArrayList, but if I code an Add(MyElement) method, the original Add(object) remains exposed, compromising type safety. I know this must be a common task, how to do it? Thanks, Gary
9
1558
by: Edward Diener | last post by:
Because 'friend' is not recognized in MC++, using the pImpl idiom in MC++ classes seems nearly impossible. Normally a pImpl class is a 'friend' to the class for which it supplies the private implementation, so that it can access any protected members, including inherited protected members, of that class. Without 'friend' the pImpl class can no longer do this, and it is a PITA passing the necessary protected data or protected member function...
11
3841
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
14
1738
by: alainpoint | last post by:
Hello, I have the need to write the equivalent of Python class methods in C++. Chuck Allison proposes the following (http://www.artima.com/cppsource/simple.html): #include <iostream> using namespace std; // A base class that provides counting
3
2126
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think meets these requirements, but after reading the chapter on exceptions in 'Exceptional C++':Sutter, I am not sure if its is really Exception safe or Exception Neutral. I suppose putting the theory in that chapter into practice isn't trivial.
1
1556
by: Bo Yang | last post by:
I have read the safe bool idiom, and the author give four defect implements. But I can't find any difference between the void* and nested_class* solution. // operator void* version class Testable { bool ok_; public: explicit Testable(bool b=true):ok_(b) {}
9
3456
by: Christian Hackl | last post by:
Hi! I've got a design question related to the combination of the NVI idiom (non-virtual interfaces, ) and popular object-oriented patterns such as Proxy or Decorator, i.e. those which have the basic idea of deriving from a base class and delegating to an object of it at the same time. My problem is that I cannot seem to combine those two techniques in a flawless way. For a very simple, non real life example (for which I shall omit...
3
3076
by: andreas.zetterstrom | last post by:
I'm implementing some different c++ classes which I want to be thread safe. All these classes contain lists or arrays of some kind. I'm using protected sections to make them thread safe. The question then is: how do you in a nice safe way pick values out of the list? The easiest way is to have a pair of Lock, Unlock functions, but this also presents a lot of ways of doing a misstake. Say the list class has 5 functions, one to get the...
0
8921
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8763
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9284
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9202
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9148
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8151
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4796
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2683
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2165
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.