C++ access to members of an inherited class, where the inherited class is a template parameter

344 Views Asked by At

I am working with the libMesh FEM library and am trying to develop a class (EqCore) that inherits from libMesh. This class will provide some additional features that are inherited again by a class that I want to actually use (MainEq).

The two functions, set_constant and get_constant, are causing the error below. These worked as shown with a different inheritance scheme (see Inheritance of template class with a template member function in C++). The difference with this problem is that now the template parameter (Type) is actually a class that gets inherited. Is this a dangerous practice?

I would appreciate any help getting this code working or finding an alternate method.

ERROR MESSAGES:

In member function ‘void EqCore::set_constant(std::string, ParamType)’: test_libmesh.cpp:26:57: error: expected primary-expression before ‘>’ token

In member function ‘ParamType EqCore::get_constant(std::string)’: /home/slaughter/Documents/programs/source/test_libmesh.cpp:31:76: error: expected primary-expression before ‘>’ token

PROGRAM:

//! \example test_libmesh.cpp

#include <string>
using std::string;

// libMesh includes
#include <libmesh.h>
#include <libmesh_common.h> 
#include <equation_systems.h>
#include <transient_system.h>
#include <explicit_system.h>
#include <parameters.h>
#include <mesh.h>
using namespace libMesh;

// Fundamental behavior that will be used among many classes
template <typename Type> class EqCore : Type{
    public:

        // Class constructor
        EqCore(EquationSystems& sys, string name) : Type(sys, name, 1){}

        // A function for storing a constant value (causes error)
        template<typename ParamType> void set_constant(std::string name, ParamType var){  
            Type::get_equation_systems().parameters.set<ParamType>(name) = var;
        }

        // A function for retrieving a constant value (causes error)
        template<typename ParamType> ParamType get_constant(std::string name){  
            ParamType output = Type::get_equation_systems().parameters.get<ParamType>(name);
            return output;
        } 
};

// A test class derived
class MainEq : public EqCore<ExplicitSystem>{
    public: 

        // Constructor
        MainEq(EquationSystems& sys) : EqCore(sys, "main"){ }   

};  


// Begin main function
int main (int argc, char** argv){

    // Initialize libMesh and create an empty mesh
    LibMeshInit init (argc, argv);
    Mesh mesh;

    // Test w/o any of the above classes
    EquationSystems eq_sys(mesh);
    eq_sys.parameters.set<double>("test1") = 1;
    printf("Test 1: %f\n", eq_sys.parameters.get<double>("test1"));

    // Test my class set/get functions
    MainEq eq(eq_sys);
    eq.set_constant<double>("test2", 2);
    printf("Test 2: %f\n", eq.get_constant<double>("test2"));   
}
2

There are 2 best solutions below

1
On BEST ANSWER

Because you are inside a template, the compiler cannot determine that set is a template automatically during parse time, and it's assuming that set is a non-template, and hence the parse fails.

The solution is to explicitly inform the compiler that set is a member template, as such.

Type::get_equation_systems().parameters.template set<ParamType>(name) = var

0
On

In C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond, by David Abrahams, Aleksey Gurtovoy (Amazon) it is explained as follows:

double const pi = 3.14159265359;

template <class T>
int f(T& x)
{
    return x.convert<3>(pi);
}

T::convert might be a member function template, in which case the highlighted code passes pi to a specialization of convert<3>. It could also turn out to be a data member, in which case f returns (x.convert < 3 ) > pi. That isn't a very useful calculation, but the compiler doesn't know it.

The template keyword tells the compiler that a dependent name is a member template:

template <class T>
int f(T& x)
{
    return x.template convert<3>(pi);
}

If we omit template, the compiler assumes that x.convert does not name a template, and the < that follows it is parsed as the less-than operator.