SYMBOL RESOLUTION IN IN-LINE DEPLOYMENT OF TEMPLATE

Asked 1 years ago, Updated 1 years ago, 92 views

Consider the following code:
1. inline Specified Template Function func
2. Class A
accessing members of Class B through func 3. Class B implementation is later than Class A

template<class T>
inline func()
{
    cout<<T::GetClassName()<endl;
}

class B;
class A {
    void Run() {
        func<B>();;
        //::B::GetClassName(); error
    }
};

classB{
public:
    static string GetClassName(){
        return "B";
    }
};

In this case,
I think the inline deployed template function needs the complete type information of class B to resolve the symbol GetClassName, but what about the C++11 standard?

By the way, when I tried gcc4.8, the above code was compiled, but when I enabled the comment out section, I was asked for complete type information and got a compilation error.

c++ c++11

2022-09-30 21:13

1 Answers

[2016-02-1801:57 Note: The following contents are likely to be misread in the specification document.See also comments]

In A::Run, func<B>() can be invoked without any problems within the C++ standard.

First, paragraph 6 of ISO/IEC 14882:2011.6.4.1 Point of Instantiation says:

The installation context of an expression that depends on the template arguments is the set of declarations with external linkage decremented priority to the point of installation of the template specialization in the same translation unit.

In this paragraph, when instantiating a template argument dependent expression (the point of instantiation in this question cut<T::GetClassName()<endl;), the instantiated point of instantiation -- the exact end cited in this question can be used.

This alone does not have the B::GetClassName declaration in front of where func<B>() was called, so it is rather nonconforming, but the seventh paragraph that follows says:

A specialization for a function template, a member function template, or of a member function or static data member of a class template may have multiple points of instantiations within a translation unit, and in addition to the points of instantiation described above, for any such specialization that has a point of instantiation within the translation unit, the end of the translation unit is also considered a point of installation.

I emphasized the key points, but roughly speaking, in specialization (specialization) such as function templates, the termination of the translation unit is considered to be an instantiated point.Together with paragraph 6, the entire translation unit declaration can be used to instantiate.
Therefore, in terms of text location, func<B>() calls precede B::GetClassName declarations, but B::GetClassName is included for specialization.


2022-09-30 21:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.