c++ - 没有返回类型的静态函数可以在 Windows 上通过编译但在 Linux 上不能

标签 c++ g++ mingw-w64

我有一个包含未声明返回类型的静态函数的类(是的,我忘了...),代码可以在 Windows 上成功编译。然而,当我尝试在我的 Arch Linux 笔记本电脑上编译时,它给了我以下错误。

错误:ISO C++ 禁止声明没有类型的“bar”[-fpermissive]

我尝试在 Windows 上使用 -Wall -fno-permissive 标志编译代码,但它仍然通过了编译并且没有任何警告。太奇怪了……

Mingw Version:
g++.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0

Compiler Information:
GNU C++14 (x86_64-posix-seh-rev0, Built by MinGW-W64 project) version 8.1.0 (x86_64-w64-mingw32)
    compiled by GNU C version 8.1.0, GMP version 6.1.2, MPFR version 4.0.1, MPC version 1.1.0, isl version isl-0.18-GMP

Default Flags:
 -iprefix C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/
 -D_REENTRANT .\Test0\hello.c -mtune=core2 -march=nocona
options enabled:  -faggressive-loop-optimizations
 -fasynchronous-unwind-tables -fauto-inc-dec -fchkp-check-incomplete-type
 -fchkp-check-read -fchkp-check-write -fchkp-instrument-calls
 -fchkp-narrow-bounds -fchkp-optimize -fchkp-store-bounds
 -fchkp-use-static-bounds -fchkp-use-static-const-bounds
 -fchkp-use-wrappers -fcommon -fdelete-null-pointer-checks -fdwarf2-cfi-asm
 -fearly-inlining -feliminate-unused-debug-types -fexceptions
 -ffp-int-builtin-inexact -ffunction-cse -fgcse-lm -fgnu-runtime
 -fgnu-unique -fident -finline-atomics -fira-hoist-pressure
 -fira-share-save-slots -fira-share-spill-slots -fivopts
 -fkeep-inline-dllexport -fkeep-static-consts -fleading-underscore
 -flifetime-dse -flto-odr-type-merging -fmath-errno -fmerge-debug-strings
 -fpeephole -fpic -fplt -fprefetch-loop-arrays -freg-struct-return
 -fsched-critical-path-heuristic -fsched-dep-count-heuristic
 -fsched-group-heuristic -fsched-interblock -fsched-last-insn-heuristic
 -fsched-rank-heuristic -fsched-spec -fsched-spec-insn-heuristic
 -fsched-stalled-insns-dep -fschedule-fusion -fsemantic-interposition
 -fset-stack-executable -fshow-column -fs
hrink-wrap-separate -fsigned-zeros
 -fsplit-i
vs-in-unroller -fssa-backprop -fstdarg-opt
 -fstrict-volatile-bitfields -fsync-libcalls -ftrapping-math -ftree-cselim
 -ftree-forwprop -ftree-loop-if-convert -ftree-loop-im -ftree-loop-ivcanon
 -ftree-loop-optimize -ftree-parallelize-loops= -ftree-phiprop
 -ftree-reassoc -ftree-scev-cprop -funit-at-a-time -funwind-tables
 -fvar-tracking -fvar-tracking-assignments -fzero-initialized-in-bss
 -m128bit-long-double -m64 -m80387 -maccumulate-outgoing-args
 -malign-double -malign-stringops -mcx16 -mfancy-math-387 -mfentry
 -mfp-ret-in-387 -mfxsr -mieee-fp -mlong-double-80 -mmmx -mms-bitfields
 -mno-sse4 -mpush-args -mred-zone -msse -msse2 -msse3 -mstack-arg-probe
 -mstackrealign -mvzeroupper

示例代码如下:

class Foo {
public:
    static bar(int a)
    {
        int ret;
        /* Do some stuff... */
        return ret;
    }
}

为什么 mingw 编译器检测不到错误?

最佳答案

Mingw 似乎可以自动检测返回类型(int)。

但无论如何这段代码都不符合C++标准,不应该被使用。

如有必要,您可以使用auto 作为返回类型

class Foo {
public:
    static auto bar(int a)
    {
        int ret;
        /* Do some stuff... */
        return ret;
    }
};

这对于任何支持 С++14 的编译器都是正确的

关于c++ - 没有返回类型的静态函数可以在 Windows 上通过编译但在 Linux 上不能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56020096/

相关文章:

c++ - 使用 mingw g++ 编译后运行 c++.exe 文件时找不到入口点错误

c++ - 具有 200GB 可用内存的 Bad Alloc c++

c++ - 是否有机会在不知道拥有队列的情况下触发 kevent?任何替代品?

macos - 如何使用 g++ 在 Mac 上使用 freeglut 库编译程序

c++ - 从结构 vector 中获取特定成员的 vector

c++ - g++ O1 不等于带有所有相关优化标志的 O0

c++ - 为什么用g++编译时会出现符号引用错误?

如果不提供 ".exe",Cmd 无法运行我的可执行文件

c++ - const 成员函数的 std 函数包装

c++ - libuv - 在没有多线程的情况下限制空闲事件的回调率而不阻塞线程