English 中文(简体)
允许海合会4.x在无助的情况下将Wreturn类作为错误处理——我们
原标题:Forcing GCC 4.x to treat -Wreturn-type as an error without enabling -Werror?

我们有以下法典:

#if !defined(__cplusplus)
#  error This file should be compiled as C++
#endif

#include <stdio.h>
#include <string>

//#define USE_CXX_CLASS
#ifdef USE_CXX_CLASS
class SomeClass
{
public:
    SomeClass() {}
    ~SomeClass() {}
    std::string GetSomeString()
    {
        // case #1
    }
};
#endif // USE_CXX_CLASS

int foo()
{
    // case #2
}

int
main (int argc, char *argv[])
{
    (void)argc;
    (void)argv;
#ifdef USE_CXX_CLASS
    SomeClass someInstance;
    someInstance.GetSomeString();
#endif // USE_CXX_CLASS
    foo();
    return 0;
}

并相信,将汇编C++编(而不是C组编)的版本4.2.1,其中可选择<代码>-Wreturn-type-Werror=return-type。 如果将上述代码汇编成册,则不必首先填写/#define USE_CXX_CLASS。 页: 1

.../gcc-4.2.1/bin/g++   -g    -fPIC -Wreturn-type -Werror=return-type    test.cpp -c -o test.o
test.cpp: In function  int foo() :
test.cpp:26: warning: control reaches end of non-void function

但是,如果<代码>/#define USE_CXX_CLASS行不通,则警告is。 a. 被视为错误:

.../gcc-4.2.1/bin/g++   -g    -fPIC -Wreturn-type -Werror=return-type    test.cpp -c -o test.o
test.cpp: In member function  std::string SomeClass::GetSomeString() :
test.cpp:18: error: no return statement in function returning non-void [-Wreturn-type]
gmake: *** [test.o] Error 1

是的,一个是非成员的职能(案例2),另一个是C++职能(案例1)。 海事组织,这不应成为问题。 我希望这两种条件都被视为错误,我不想在此刻添加<代码>-Werror或-Wall(或许会以后这样做),但这不属于这一问题的范围。

我的分点是:

  1. Is there some GCC switch that I am missing that should work? (No I do not want to use #pragma s.)
  2. Is this a bug that has been addressed in a more recent version of GCC?

For reference, I have already poured through other similar questions already, including the following:

最佳回答

它是固定的,与<代码>g++良好。 9.3 :成员职能和自由职能均作为错误对待,编号为<>-Wall-Werror=return-type。

问题回答

I do see an error even w/o the USE_CXX_CLASS flag. i.e. g++ is consistent with the error for both class member functions and non member functions. g++ (GCC) 4.4.3 20100127 (Red Hat 4.4.3-4)

在我看来,你所需要的是一把手脚包。

  • Name it something like gcc-wrapper and g++-wrapper.
  • In your Makefile set CC and CXX to the wrappers.
  • Have the wrapper invoke GCC and pipe its output to another program which will search for your desired warning strings.
  • Have the search program exit with an error when it finds the warning.




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签