简体   繁体   English

为什么带有模板 arguments 的 C++ function 在 clang++ 7 上没有警告“控制到达非无效函数的末尾”?

[英]why doesn't a C++ function with template arguments warn “control reaches the end of a non-void function” on clang++ 7?

I noticed that if I compile this我注意到如果我编译这个

int x() { }

I get a warning, as usual:像往常一样,我收到警告:

$ clang++-7 -pthread -std=c++17 -o main main.cpp
main.cpp:5:11: warning: control reaches end of non-void
      function [-Wreturn-type]
int x() { }
          ^
1 warning generated.

$ ./main

however, if the function takes any template arguments:但是,如果 function 采用任何模板 arguments:

template <typename y>
int x() { }

no warnings appear.没有警告出现。

$ clang++-7 -pthread -std=c++17 -o main main.cpp
$ ./main

Is this a bug in the compiler?这是编译器中的错误吗?

Until you call (or otherwise odr-use) that function template specialisation, instantiating it, the actual resulting function does not exist, and you won't get any diagnostics about it unless it is syntactically invalid.直到您调用(或以其他方式使用) function 模板专业化,实例化它,实际生成的function不存在,除非它在语法上无效,否则您不会得到任何关于它的诊断。

We cannot see the rest of your program as you did not think a reproducible example was important, but presumably you did not do this in your program.我们看不到您的程序的 rest,因为您认为可重现的示例并不重要,但可能您没有在程序中这样做。

Write x<int>();x<int>(); in your main function and you'll see the warning you wanted.在你的主要 function 中,你会看到你想要的警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM