Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoogleTest with Templates

Hope this question will not be too stupid. I'm quite new in using gtest and it's been quite a very long time that I don't make use of C++. Let suppose w have this simplistic template Point class:

template <class T>
    struct Point
{
    union
    {
        T _data [2];
        struct { T x, y; };
    };
    constexpr Point(T x_, T y_) noexcept : x(x_), y(x_) {}
};

Then using gtest, I'm trying to check if Point is default constructible:

TEST_P(PointTest,Concept)
{
    ASSERT_TRUE(std::is_nothrow_constructible<Point<float>,float,float>::value);
}

Then I get this compilation error with Clang 6.0.1 with C++11 flags:

"error: too many arguments provided to function-like macro invocation"

Any help will be welcome. I do know that special care must be taken when combining templates and googletest. But I didn't figure out a solution. Thank you !

like image 560
Cherubin Avatar asked Sep 06 '25 03:09

Cherubin


1 Answers

I suppose it's a problem with C++ preprocessor that recognize the commas between templates arguments

// ...................................................v.....v
ASSERT_TRUE(std::is_nothrow_constructible<Point<float>,float,float>::value);

as macros arguments separators.

I suggest to calculate the value outside the macro call

TEST_P(PointTest,Concept)
{
   constexpr auto val 
      = std::is_nothrow_constructible<Point<float>, float, float>::value;

   ASSERT_TRUE( val );
}

or to pass through a using for the type

TEST_P(PointTest ,Concept)
{
   using inc = std::is_nothrow_constructible<Point<float>, float, float>;

   ASSERT_TRUE( inc::value );
}
like image 160
max66 Avatar answered Sep 08 '25 23:09

max66