没有返回类型的静态函数可以通过Windows上的编译,但不能传递Linux上的编译

Static function without return type can pass compile on Windows but not on Linux

本文关键字:编译 但不能 Linux Windows 返回类型 静态函数 可以通过      更新时间:2023-10-16

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

error: ISO C++ forbids declaration of 'bar' with no type [-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 .Test0hello.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

的编译器,这都是正确的
相关文章: