Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板类型化测试方法_C++_Templates_Googletest - Fatal编程技术网

C++ 模板类型化测试方法

C++ 模板类型化测试方法,c++,templates,googletest,C++,Templates,Googletest,这就是我所拥有的: /* Can't change 'base' struct. */ struct base { public: template<typename T> void printVal() { std::cout << T::x << std::endl; } }; struct testFixture : public base , public ::testing::Test { usi

这就是我所拥有的:

/* Can't change 'base' struct. */
struct base {
  public:
    template<typename T>
    void printVal() {
      std::cout << T::x << std::endl;
    }
};

struct testFixture
  : public base
  , public ::testing::Test {
  using base::printVal;
};

TEST_F(testFixture, testF) {
  printVal<A>();
  printVal<B>();
  printVal<C>();
}
有没有一种方法可以通过googletest获得这种类型的功能?

找到了答案

“模板化”夹具:

template <typename T>
struct testFixture
  : public base
  , public ::testing::Test {
  using base::printVal;
  void printVal() {
    base::printVal<T>();
  }
};
模板
结构测试夹具
:公共基地
,public::testing::Test{
使用base::printVal;
void printVal(){
base::printVal();
}
};
然后继续正常操作:

typedef ::testing::Types<A, B, C> MyTypes;
TYPED_TEST_CASE(testFixture, MyTypes);

TYPED_TEST(testFixture, typedTest) {
  this->template printVal<TypeParam>();
}
typedef::testing::Types MyTypes;
类型化测试用例(testFixture,MyTypes);
类型测试(测试夹具、类型测试){
此->模板printVal();
}
template <typename T>
struct testFixture
  : public base
  , public ::testing::Test {
  using base::printVal;
  void printVal() {
    base::printVal<T>();
  }
};
typedef ::testing::Types<A, B, C> MyTypes;
TYPED_TEST_CASE(testFixture, MyTypes);

TYPED_TEST(testFixture, typedTest) {
  this->template printVal<TypeParam>();
}