我有一个包含静态函数的类,该函数未声明返回类型(是,我忘记了...),代码可以在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 .\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编译器无法检测到错误?
答案 0 :(得分:0)
Mingw似乎能够自动检测返回类型(int)。
但是在任何情况下,此代码均不符合C ++标准,因此不应使用。
如有必要,您可以使用自动作为返回类型,例如
class Foo {
public:
static auto bar(int a)
{
int ret;
/* Do some stuff... */
return ret;
}
};
这对于任何支持С++ 14的编译器都是正确的