I am studying the answer of How to test c++ template class with multiple template parameters using gtest? . But I cannot compile the code from the answer there. To test the code, I created a minimal (unfortunately not working) example.
EDIT: I update the code again (considering the comment from Marko Popovic):
#include <iostream>
#include "gtest/gtest.h"
template <typename A>
struct whoami { void tellme(){printf("I do not know!");} };
template <>
struct whoami<int> { void tellme(){printf("I am an integer!");} };
template <>
struct whoami<char> { void tellme(){printf("I am a character!");} };
template <>
struct whoami<bool> { void tellme(){printf("I am a boolean!");} };
template <class A, class B>
struct TypeDefs
{
typedef typename A firstType;
typedef typename B secondType;
};
template <class T>
class ATestExample : public testing::Test
{
protected:
ATestExample() {}
virtual ~ATestExample(){ }
};
typedef ::testing::Types <TypeDefs<char,char>, TypeDefs<int,int> > MyTypeList;
TYPED_TEST_CASE(ATestExample, MyTypeList);
TYPED_TEST(ATestExample, DefaultConstructor)
{
whoami<TypeParam::firstType> info;
info.tellme();
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
But it still gives the error:
debuging.cpp(..): error: a class or namespace qualified name is required
debuging.cpp(..): error: a class or namespace qualified name is required
debuging.cpp(..): error: nontype "gtest_TypeParam_::firstType" is not a type name
detected during:
implicit generation of "ATestExample_DefaultConstructor_Test<gtest_TypeParam_>::~ATestExample_DefaultConstructor_Test() [with gtest_TypeParam_=TypeDefs<char, char>]"
gtest/gtest.h(7209): here
instantiation of class "ATestExample_DefaultConstructor_Test<gtest_TypeParam_> [with gtest_TypeParam_=TypeDefs<char, char>]"
gtest/gtest.h(7209): here
implicit generation of "ATestExample_DefaultConstructor_Test<gtest_TypeParam_>::ATestExample_DefaultConstructor_Test() [with gtest_TypeParam_=TypeDefs<char, char>]"
gtest/gtest.h(7209): here
instantiation of class "ATestExample_DefaultConstructor_Test<gtest_TypeParam_> [with gtest_TypeParam_=TypeDefs<char, char>]"
Edit 2: The solution is a position-permutation of "typename":
template <class A, class B>
struct TypeDefs
{
typedef A firstType;
typedef B secondType;
};
template <class T>
class ATestExample : public testing::Test
{
protected:
ATestExample() {}
virtual ~ATestExample(){ }
};
typedef ::testing::Types <TypeDefs<cpu,char>, TypeDefs<gpu,int> > MyTypeList;
TYPED_TEST_CASE(ATestExample, MyTypeList);
TYPED_TEST(ATestExample, DefaultConstructor)
{
whoami<typename TypeParam::firstType> info;
info.tellme();
}