Re: Template Inheritance ?

In the general case, the scope resolution operator would be needed for var as it's private to the parent.

Here on top of that you are using templates and when the compiler encounters a dependent name within a template class, it waits until the template is instantiated with specific template arguments to determine the scope of the name and so the compiler can't compile the class as it is.

When you use the scope resolution operator ::, you inform the compiler that var is a member of the base class, allowing it to resolve the name correctly.

Yes it was just part of the pb
The real challenge is the way the compiler finds the right variable

The notation below compiles (once I implemented the default constructor for StreamReporter and defined STREAM_HANDLER_BUFFER_SIZE):

        last = this->var;
        this->display(out);

Why? I can't explain.

The answer I think is in Unqualified name lookup - cppreference.com

I think this is what applies

Template definition

For a non-dependent name used in a template definition, unqualified name lookup takes place when the template definition is examined. The binding to the declarations made at that point is not affected by declarations visible at the point of instantiation. For a dependent name used in a template definition, the lookup is postponed until the template arguments are known, at which time ADL examines function declarations with external linkage(until C++11) that are visible from the template definition context as well as in the template instantiation context, while non-ADL lookup only examines function declarations with external linkage(until C++11) that are visible from the template definition context (in other words, adding a new function declaration after template definition does not make it visible except via ADL). The behavior is undefined if there is a better match with external linkage in the namespaces examined by the ADL lookup, declared in some other translation unit, or if the lookup would have been ambiguous if those translation units were examined. In any case, if a base class depends on a template parameter, its scope is not examined by unqualified name lookup (neither at the point of definition nor at the point of instantiation).

Hence my initial attempt at explaining it

That’s it

@Delta_G
Could you provide an example how to instantiate OnChangeVariableReporter class in the code?
I failed to do that without a errors

Assuming it was just an error, I made the constructor for that class public instead of private. I was then able to instantiate an object in the main code .... after I fixed the other errors.

1 Like

Perhaps, but I wanted to test compile the code you actually posted. Compiling just the templated class declaration without attempting to instantiate an object is not a very thorough test. There are errors that only show up when you try to create the object. So, I took the expediency of making the constructor public, enabling the main code to do an instantiation.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.