Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable of a template class with a template class template parameter set to a base template of the derived template with the variable

Tags:

c++

templates

I'm attempting to have a derived class (normal template) that has a variable of a template type that has as its template class parameter the type of a base class (normal template, same parameter as the derived class) of the derived class (the one with the variable). This makes VC++ incredibly angry at me, and I am incapable of calming its fury. Here's a quick example:

template<template<typename VT> class CT, typename VT> struct encapThing {};

template<typename VT> struct innocuousBase {};

template<typename VT> struct derivOfDoom : public innocuousBase<VT>
{
    encapThing<innocuousBase, VT> ohgodhelp; //C3200
};

It will throw a C3200, saying it expected a class template. Now, I can see why this might be thinking there is a recursive loop of templates within templates, even if this isn't actually the case. How can I convince VC++ otherwise?

like image 591
user173342 Avatar asked Jun 07 '11 20:06

user173342


People also ask

Can a template be a template parameter?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)

What is a template template parameter in C++?

In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

How many template parameters are allowed in a template classes?

Explanation: Just like normal parameters we can pass more than one or more template parameters to a template class.

Why do we use template template parameters Mcq?

8. Why we use :: template-template parameter? Explanation: It is used to adapt a policy into binary ones. 9.


1 Answers

Unqualified use of innocuousBase inside of derivOfDoom<> is interpreted as innocuousBase<VT>, much as unqualified use of derivOfDoom in that context would be interpreted as derivOfDoom<VT>. I don't remember offhand whether or not this is standard-conformant behavior, but the workaround is trivial: fully qualify innocuousBase so the compiler knows you're referring to the innocuousBase class template and not the innocuousBase<VT> base class:

template<typename VT> struct derivOfDoom : innocuousBase<VT>
{
    encapThing<::innocuousBase, VT> ohgodhelp;
};
like image 150
ildjarn Avatar answered Sep 17 '22 15:09

ildjarn