toeb / googletest

Automatically exported from code.google.com/p/googletest
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

GCC compilation error "error: conversion from `void' to non-scalar type" #201

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
A compilation error is generated on GCC when using ASSERT_* macros inside
of a method that does not return void.  Two types of error messages can be
generated.  If the return type is a complex data type (i.e. class) the
following error occurs:

*error: conversion from `void' to non-scalar type `null-type'*

If the return type is a POD type (i.e. int) the following error occurs:

*error: void value not ignored as it ought to be*

The following code can be used to generate the errors.  I've tested it on
GCC versions 3.4.3 and 4.3.3.

{{{
#include <gtest/gtest.h>

struct null_type {};

null_type function_return_struct()
{
   ASSERT_TRUE(true);

   return null_type();
}

int function_return_POD()
{
   ASSERT_TRUE(true);

   return 0;
}

TEST(GCC, COMPILE_ERRORS)
{
   function_return_struct();
   function_return_POD();
}
}}}

I looked through the code and it seems the culprit is in the file
[http://code.google.com/p/googletest/source/browse/trunk/include/gtest/internal/
gtest-internal.h#781
gtest/internal.h @ line 781].  It looks like the easy fix is to remove the
extraneous return statement.

Original issue reported on code.google.com by trevor.p...@gmail.com on 21 Sep 2009 at 7:48

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Hmm, I guess wiki syntax doesn't work in the issue tracker. :(

Original comment by trevor.p...@gmail.com on 21 Sep 2009 at 7:50

GoogleCodeExporter commented 9 years ago
I just realized that this change would affect the flow of test execution since 
it
assumes a return on fatal assertions. I'm not sure what the best way to handle 
this
currently since the return is used to stop the flow of execution.  A possibility
would be to throw an exception for a fatal assertion but this will break 
environments
where exception handling is not compiled in. Any ideas?

Original comment by trevor.p...@gmail.com on 22 Sep 2009 at 3:39

GoogleCodeExporter commented 9 years ago
This is the designed behavior.  Please see 
http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide#Assertion_Place
ment 
(the Assertion Placement section) for details.

Original comment by zhanyong...@gmail.com on 22 Sep 2009 at 7:22