There is no suitable default constructor available in googletest EXPECT_NO_THROW

Declaration:

class ClassOne { ClassOne (ClassTwo* classTwo, ClassThree const& classThree); } 

Test:

 ClassTwo* classTwo; ClassThree classThree; EXPECT_NO_THROW (ClassOne (classTwo, classThree)); 

This compiles and runs, but now I change it to:

Declaration:

 class ClassOne { ClassOne (ClassThree const& classThree); } 

Test:

 ClassThree classThree; EXPECT_NO_THROW (ClassOne (classThree)); 

This does not work with a "suitable default constructor inaccessible."

The following lines are compiled:

 ClassOne classOne (classTwo, classThree); // First case ClassOne classOne (classThree); // Second case 

Is there any reason why I can't EXPECT_NO_THROW for a constructor with one parameter?

+4
source share
2 answers

This is a bug in gtest, I think (although I'm not a macro expert). EXPECT_NO_THROW eventually expands to:

 #define GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) \ if (::testing::internal::AlwaysTrue()) { statement; } 

Your code compiles using VS2012RC if statement enclosed in parentheses in the body of if :

 #define GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) \ if (::testing::internal::AlwaysTrue()) { (statement); } // ^ ^ 

As a workaround, you can:

 EXPECT_NO_THROW ((ClassOne (classThree))); 
+4
source

You were bitten by C ++ the most unpleasant analysis . The "statement" argument of the EXPECT_NO_THROW macro, ClassOne (classThree) , is not a definition of an unnamed ClassOne object; the constructor receives a ClassThree object with the name classThree . This is the default declaration of the constructed ClassOne object, called classThree . This is the same as if you wrote EXPECT_NO_THROW (ClassOne classThree); - Parents are optional.

See https://youtu.be/lkgszkPnV8g?t=1750.

The solution (if you can use C ++ 11) is to use uniform initialization:

EXPECT_NO_THROW (ClassOne {classThree});

0
source

Source: https://habr.com/ru/post/1416342/


All Articles