Link to home
Start Free TrialLog in
Avatar of sparkythedog
sparkythedogFlag for Canada

asked on

Trick to generate compiler error if "wrong" function called

In C++ does anybody know a trick to ensure that a function is never called though its definition does exist? The compiler would generate an error if this function is called.

I have a hierarchy of classes that contain a parallel template inner classes hierarchy. These inner classes have a virtual function func(). I have a problem overriding this function in the derived specialized inner class.

This might be a separate issue but for now I would just be interested to know if a technique exists to catch a call to a "wrong" function in the compile time.
Avatar of efn
efn

All I can think of is to make the function a private member of a class and make sure no other class member function calls it.  That will make it so the function exists, but nobody else can call it.
Avatar of sparkythedog

ASKER

Well, unfortunately the function cannot be made private. It is a public member function of a protected inner template class.

What happens is that I use a derived inner class (and it is derived from a specialized version of a base inner template class). I need to override the inner class public virtual function FUNC() for this specialization.

As for the base inner template class, I would not even need its version of FUNC() for this specific kind of specialization. It is required to be in place by the compiler but I would liketo have it prevented from accidental use.

Hope I clarified my problem a bit.
Avatar of Infinity08
Why not just let the function do "nothing" ?
you should make it pure virtual

it took a look bit of prodding but here's some sample code.

#include<iostream>

class base_outerclass
{
public:


      base_outerclass(){}
      void callsomefunction();

protected:
      class base_innerclass
      {
      public:
            base_innerclass(){}
            virtual void somefunction()=0;
      };

      base_innerclass* bi;

};

class derived_outerclass : public base_outerclass
{
public:
      derived_outerclass(){this->bi = &di;}
protected:
      class derived_innerclass :public base_outerclass::base_innerclass
      {
      public:
            virtual void somefunction();
      };
      derived_innerclass di;

};

void base_outerclass::callsomefunction()
{
      bi->somefunction();
}
      
      
void derived_outerclass::derived_innerclass::somefunction()
{
      std::cout << "hello world\n";
}

int main()
{
      base_outerclass* p_bo= new derived_outerclass();
      p_bo->callsomefunction();
      delete p_bo;

}
>>>> you should make it pure virtual
pure virtual functions that have an implementation nevertheless can be called if not private:

class Base
{

public:
      virtual void func() = 0
      {   // implementation }

};

class Derived : public Base
{
};

int main
{
     Derived d;
     d.Base::func();   // compiles

     return 0;
}:

sparkythedog, why can't you make it private?

You told it should not be called but as you said you can't make it private I assume it is called nevertheless. Right?

Regards, Alex



>>>>>>>> you should make it pure virtual
>>>>pure virtual functions that have an implementation nevertheless can be called if not private:

I am assuming that he would remove the implementation if he had a way to override it.  I am guessing that's what he was alluding to with the other problem of not being able to override.  My code may guide him, but because he's using templated classes there may be other problems I didn't foresee especially since template functions can't be virtual

Admittedly, without the code though, I am making a lot of assumptions.
>>>> does anybody know a trick to ensure that a function
>>>> is never called though its definition does exist?

You can make the function a template function by setting template <typename T> above the function code. If someone tries to call it, there is a compile error that the template type cannot be determined.

template <typename T>
void functionNeverCalled()
{
      // implementation
}

int main()
{
    functionNeverCalled();   // error: cannot determine template type
    return 0;
}

Regards, Alex
>>functionNeverCalled();   // error: cannot determine template type


functionNeverCalled<int>(); // hey, i can call it like this :-)
>>>> functionNeverCalled<int>(); // hey, i can call it like this :-)

put this line at top of the implementation of the template

      class X {    X() {} } x;// error ==> cannot call private constructor

or any other invalid statement.
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Guys,

Thanks a lot for your replies. It does not seem I can use any of the recommendations since:

a) It cannot be private. My virtual function is part of a class public interface. To simplify the design, the hierarchy is class Abstract -> class A -> class B -> class C. If func() is declared as virtual public in A you cannot redecalre it as private in B. (or rather you can but I guess it will still be publicly accessible due to its virtuality.

b) It cannot do nothing. It is actually a factory method. If it becomes part of the interface you cannot guarantee that somebofy will not (mistakenly) use it.

c) I do not think I can make it pure virtual. It is declared pure virtual in Abstract, then it is defined in A, B and C. If I try to make it pure virtual in B I am not sure it will compile. At least in a simplified code that I tried to compile it did not work.

d) I am not comfortable either with the idea of making the function templated. The code as it is is overburdened with templates, template template parameters etc. I would like to avoid additional complexity. Besides even if it is templated I do not quite see how in the hierarchy Abstract->A->B->C it would be callable in A and C, but not in B.

e) I start to feel that no good answer means that probably the question itself is flawed. Maybe I am looking in the wrong direction. I will post an example of the original problem with overriding. I would appreciate it a lot if some of the experts could point out to me where the problem is.

Thanks again for your insights.