开发者

Function templates and Private Inheritance

开发者 https://www.devze.com 2023-02-13 05:43 出处:网络
I recently ran into a problem when using a private inheritance scheme in which the base class defined a template method and the (privately) derived class made that method public via a using declaratio

I recently ran into a problem when using a private inheritance scheme in which the base class defined a template method and the (privately) derived class made that method public via a using declaration under the public access specifier. The template was designed to take the address of a function and invoke that function pointer. However, upon attempting to pass the name of the function to the derived class template method, I receive an error message that states that the base-class method cannot access a private member declared in the derived class. Here is a code segment that models the issue:

class A
{ 
public:
     template<class T> void Funct(T pFunct) { }
};

class B : private A
{
public:
    using A::Funct;
};

void Show(void) { }

int main(void)
{
    B b;
    b.Funct(Show);
}

The exact resulting error message is: 'A::Funct' : cannot access private member declared in class 'B'

I am able to resolve the issue simply by:

1)Preceding the function argument name with the address-of operator:

b.Funct(&Show);

2)Explicitly qualifying the template type argument:

b.Funct<void(*)(void)>(Show)

If the Show() function were a template as well, I would need to explicitly qualify the template using the proper template type argumen开发者_如何学Pythonts used to instantiate Show.

My question is not how to solve the problem but why the error message is being generated. Why does instantiating Funct() with Show versus with &Show cause the compiler to do two different things. And why does instantiating Funct() with Show cause the Funct() method to attempt to access the private data in class B (which I'm assuming is the A subobject in class B)?


Compiles fine with Comeau Online. Ergo, compiler bug. Report it.

Cheers & hth.,


bcc32 gives error: error bccE2247: 'Funct<void (*)()>(void (*)())' is not accessible in function main()

I believe this is a compiler bug.

0

精彩评论

暂无评论...
验证码 换一张
取 消