I want to do this:
template <enum Type>
class Message {
private:
Type m_type
};
enum StdInMessages { PrintHello, Echo, ... };
class StdInMsg : Message<StdInMessages>
{ ... }
enum NetworkMessages { DoSomethingElse, Shutdown ... };
class NetworkMsg : Message<NetworkMessages>
{ ... }
Of course, the actual messages are slightly different
Why doesn't this work?
template <enum T> class ATemplate {};
I get this error
error: use of enum ‘T’ without previous declaration
It works if enum T is declared beforehand:
enum T {
foo, bar
};
template <enum T> // or simply `template <T>`
class ATemplate { };
int main() {
ATemplate<foo> x;
}
But judging from the variable name T, this isn’t what you want. Since it’s hard to guess what exactly you want, you need to be more specific.
Because that's not valid syntax for a template unless what you're looking for is what Konrad answered.
You need to either use typename or class.
The following should do it:
enum X
{
a
};
template <typename T> class ATemplate {};
ATemplate<X> A;
Related
I have a Test class which is templated on two enums of the same enum type.
I'm trying to write a specialization for this Test class for when the two enum values are the same.
enum class Enum
{
A,
B
};
template <Enum ENUM_1, Enum ENUM_2>
class Test {};
template <Enum ENUM>
class Test<ENUM, ENUM> {};
int main()
{
Test<Enum::A> test;
}
The above results however in the following error:
main.cpp:23:5: error: too few template arguments for class template 'Test'
Test<Enum::A> test;
^
main.cpp:13:7: note: template is declared here
class Test
^
1 error generated.
What is wrong with the above code?
Test requires exactly two template parameters. Specializing doesn't remove ENUM_2. If you want to instantiate Test with a single type and use it for ENUM_2 as well, you can define a default for ENUM_2:
template <Enum ENUM_1, Enum ENUM_2 = ENUM_1>
class Test {};
Laying out the solution from the accepted response in case it helps others:
template <Enum ENUM_1, Enum ENUM_2 = ENUM_1>
class Test
{
public:
Test()
{
std::cout << "base\n";
}
};
template <Enum ENUM>
class Test<ENUM, ENUM>
{
public:
Test()
{
std::cout << "special\n";
}
};
int main()
{
Test<Enum::A, Enum::B> test_AB{};
Test<Enum::A, Enum::A> test_AA{};
Test<Enum::A> test_A{};
}
prints out
base
special
special
I want to use 'MyType' from the base class in the 'DoesBlah' test below.
#include <gtest/gtest.h>
template <typename T>
struct MemberVariable
{
T m_t;
};
struct Base : public ::testing::Test
{
template <typename MemberType>
using MyType = MemberVariable<MemberType>;
};
template <typename DerivedType>
struct Derived : public Base
{
};
typedef ::testing::Types<int, char> MyTypes;
TYPED_TEST_CASE(Derived, MyTypes);
TYPED_TEST(Derived, DoesBlah)
{
MyType<TypeParam> test;
test.m_t = (TypeParam)1;
ASSERT_EQ(test.m_t, 1);
}
However, I get the following compilation error:
gti/specific/level/Test.t.cpp: In member function 'virtual void Derived_DoesBlah_Test<gtest_TypeParam_>::TestBody()':
gti/specific/level/Test.t.cpp:25:5: error: 'MyType' was not declared in this scope
MyType<TypeParam> test;
I tried using TestFixture::MyType, typename TestFixture::MyType, but both did not work.
How can I get Derived to recognize that there's something called 'MyType'?
With some simplifications, the macro TYPED_TEST(Derived, DoesBlah) expands to something like:
template <typename TypeParam>
class Derived_DoesBlah_Test : public Derived<TypeParam>
{
private:
typedef Derived<TypeParam> TestFixture;
virtual void TestBody();
};
template <typename TypeParam>
void Derived_DoesBlah_Test<TypeParam>::TestBody()
So the {} block that follows is the function definition for a member of a template class which derives from Derived<TypeParam>. The typedef for TestFixture is available, but it depends on the template parameter TypeParam, so it is considered a dependent type. What's more, you want to access a template member of that dependent type. So you need both the typename and template keywords:
{
typename TestFixture::template MyType<TypeParam> test;
test.m_t = (TypeParam)1;
ASSERT_EQ(test.m_t, 1);
}
For more about dependent types and using the typename and template keywords in declarations and expressions, see this SO question.
Given the following two structs, one could derive from both nested 'Nested' classes, and call foo() and bar() from the derived object:
struct WithNested1 {
template<class T> struct Nested {
void foo();
};
};
struct WithNested2 {
template<class T> struct Nested {
void bar();
};
};
struct Test : WithNested1::Nested<Test>,
WithNested2::Nested<Test>
{
};
Test test;
test.foo();
test.bar();
But, if both of the outer classes were passed as variadic template arguments, how would you derive from them?
For example, this fails to compile:
template<typename... Ts>
struct Test : Ts::template Nested<Test>...
{
};
Test<WithNested1, WithNested2> test;
test.foo();
test.bar();
error: 'foo' : is not a member of 'Test'
error: 'bar' : is not a member of 'Test'
strangely, it compiles if the calls to foo() and bar() are removed.
template <typename... Ts>
struct Test : Ts::template Nested<Test<Ts...>>...
{
};
This is the same answer as above but I figured I'd explain how it works. First in your example Test has no template param (which the compiler should warn you of), but which should we give it. The point of CRTP is to give the class you inherit from a template param that is the same as your type, that way it has access to your methods and members through the of the template param. Your type in this case is Test<Ts...> so that is what you have to pass it. As #aschepler already pointed out normally you could use Test by itself but it's not in scope until your already inside the class.
I think this is a cleaner way of doing what you want.
template <typename T>
struct A {
void bar (){
static_cast<T*>(this)->val = 3;
}
};
template <typename T>
struct B {
void foo (){
static_cast<T*>(this)->val = 90;
}
};
template <template<class> class ... Ts>
struct Test : Ts<Test<Ts...>>...
{
int val;
};
int main() {
Test<A,B> test;
test.foo();
test.bar();
return 0;
}
The "injected class name" Test which can be used as an abbreviation of Test<Ts...> is not in scope where you tried to use Nested<Test>, since the class scope does not begin until the { token.
Use
template<typename... Ts>
struct Test : public Ts::template Nested<Test<Ts...>>...
{
};
This works:
template<typename... Ts>
struct Test : Ts::template Nested<Test<Ts...>>...
// ^^^^^^^
{
};
9/2:
[...]. The class-name is also inserted into the scope of the class itself; this is known as the injected-class-name. For purposes of access checking, the injected-class-name is treated as if it were a public member name. [...]
14.6.1/1:
Like normal (non-template) classes, class templates have an injected-class-name (Clause 9). The injectedclass-name can be used as a template-name or a type-name. When it is used with a template-argument-list, as a template-argument for a template template-parameter, or as the final identifier in the elaborated-typespecifier of a friend class template declaration, it refers to the class template itself. Otherwise, it is equivalent to the template-name followed by the template-parameters of the class template enclosed in <>.
Consider the following code :
template<bool AddMembers> class MyClass
{
public:
void myFunction();
template<class = typename std::enable_if<AddMembers>::type> void addedFunction();
protected:
double myVariable;
/* SOMETHING */ addedVariable;
};
In this code, the template parameter AddMembers allow to add a function to the class when it's true. To do that, we use an std::enable_if.
My question is : is the same possible (maybe with a trick) for data members variable ? (in a such way that MyClass<false> will have 1 data member (myVariable) and MyClass<true> will have 2 data members (myVariable and addedVariable) ?
A conditional base class may be used:
struct BaseWithVariable { int addedVariable; };
struct BaseWithoutVariable { };
template <bool AddMembers> class MyClass
: std::conditional<AddMembers, BaseWithVariable, BaseWithoutVariable>::type
{
// etc.
};
First off, your code just won't compile for MyClass<false>. The enable_if trait is useful for deduced arguments, not for class template arguments.
Second, here's how you could control members:
template <bool> struct Members { };
template <> struct Members<true> { int x; };
template <bool B> struct Foo : Members<B>
{
double y;
};
It appears to me that C++ does not allow member template specialization in any scope other than namespace and global scope (MS VSC++ Error C3412). But to me it makes sense to specialize a base class's primary member template in the derived class because that is what derived classes do - specialize things in the base class. For instance, consider the following example:
struct Base
{
template <class T>
struct Kind
{
typedef T type;
};
};
struct Derived : public Base
{
/* Not Allowed */
using Base::Kind;
template <>
struct Kind <float>
{
typedef double type;
};
};
int main(void)
{
Base::Kind<float>::type f; // float type desired
Derived::Kind<float>::type i; // double type desired but does not work.
}
My question is why isn't it allowed?
I get what you're trying to do, but you are not doing it right. Try this :
struct Base{};
struct Derived{};
// Original definition of Kind
// Will yield an error if Kind is not used properly
template<typename WhatToDo, typename T>
struct Kind
{
};
// definition of Kind for Base selector
template<typename T>
struct Kind<Base, T>
{
typedef T type;
};
// Here is the inheritance you wanted
template<typename T>
struct Kind<Derived, T> : Kind<Base, T>
{
};
// ... and the specialization for float
template<>
struct Kind<Derived, float>
{
typedef double type;
};
My question is why isn't it allowed?
From my copy of the draft it appears that the following puts the above restriction:
In
an explicit specialization declaration for a class template, a member of a class template or a class member
template, the name of the class that is explicitly specialized shall be a simple-template-id.
The workaround is to specialize the enclosing class.
I will "ignore" the standard specifications and try a logical argument:
If you have two classes:
class A
{
struct S { };
};
class B: public A
{
struct S { };
};
A::S and B::S are two different types. Extending the logic to the template specializations, when you try to specialize an inner class declared in base class through an inner class in derived class, you actually are trying to define a different type, with the same name (but another naming scope).