首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不带返回类型的静态函数可以在Windows上传递编译,但不能在Linux上传递

不带返回类型的静态函数可以在Windows上传递编译,但不能在Linux上传递
EN

Stack Overflow用户
提问于 2019-05-07 10:01:46
回答 1查看 96关注 0票数 1

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

error: ISO C++ forbids declaration of 'bar' with no type [-fpermissive]

我试图在Windows上使用-Wall -fno-permissive标志编译代码,但它仍然在没有任何警告的情况下通过了编译和。太奇怪了..。

代码语言:javascript
复制
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

下面是示例代码:

代码语言:javascript
复制
class Foo {
public:
    static bar(int a)
    {
        int ret;
        /* Do some stuff... */
        return ret;
    }
}

为什么混合编译器不能检测到错误?

EN

回答 1

Stack Overflow用户

发布于 2019-05-07 10:28:04

看来明威能够自动检测返回类型(int)。

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

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

代码语言:javascript
复制
class Foo {
public:
    static auto bar(int a)
    {
        int ret;
        /* Do some stuff... */
        return ret;
    }
};

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56020096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档