0

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();

}
Community
  • 1
  • 1

1 Answers1

0

You have two errors. The first one is that you have named your templated struct Types, which creates a problem when it is used with ::testing::Types. Lets assume you rename it to TypeDefs. Then, in the body of the test you have an error when declaring a typedef because you have not specified template parameters. You must do this:

typedef TypeDefs<char, int>::firstType someType;

Do not use typedef inside TYPED_TEST, use TypeParam directly:

whoami<TypeParam::firstType> info;
info.tellme();
Marko Popovic
  • 3,999
  • 3
  • 22
  • 37
  • But this is not the answer of the compiler error. I changed the variable- and type-naming in my question. `TypeParam` will contain the current type in the TestBody. –  Nov 23 '15 at 09:30
  • @wieschoo I did not notice you've edited the question. The edits changed the question a lot. A completely different compiler error is generated in this new version of code. I've edited my answer accordingly. – Marko Popovic Nov 23 '15 at 09:52
  • Nope. The compiler error is still the same as in my original question, although you were correct by not using `Types` as a name in my code. –  Nov 23 '15 at 10:24
  • @wieschoo Hmm, are you using latest versions of the compilers and googletest. I am using latest MSVC compiler and version 1.7.0 of googletest, and this code compiles without any problems ... – Marko Popovic Nov 23 '15 at 10:40
  • maybe it's again a MSVC feature? I use `g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4` –  Nov 23 '15 at 11:10
  • @wieschoo It is possible, but I must admit that I am not sure. The answer probably lies in the underlying implementation of `TYPED_TEST_CASE` and `TYPED_TEST`. – Marko Popovic Nov 23 '15 at 13:16