简体   繁体   English

为什么不从void函数模板返回编译器错误?

[英]Why don't I get compiler errors from returning from a void function template?

Consider: 考虑:

void f() {
    return 5;
}

The above will raise errors. 以上会引起错误。 But why not this?: 但为什么不呢?:

template <typename = void> void f() {
    return 0;
}

I'm compiling with gcc-4.5.1. 我正在使用gcc-4.5.1进行编译。 Why does it make a difference using templates such that I wouldn't receive errors from doing the same illegal return statement as a non-template function?. 为什么使用模板会产生差异,以至于我不会因为非模板函数执行相同的非法返回语句而收到错误? The only setback I get is that I can't call the function (ie f() ) without getting: 我得到的唯一挫折是我无法调用函数(即f() )而不得:

error: return-statement with a value, in function returning 'void'

But still, what could be the reason that I am able to define a return statement for a void function template? 但是,我能够为void函数模板定义return语句的原因是什么?

Here is the code I have: 这是我的代码:

template <typename = void> void f() {
    return 0;
}

// pass

int main() {



}

The above code will pass despite a presumably illegal return statement in a function returning void. 尽管在返回void的函数中有一个非法的返回语句,上面的代码仍将通过。

Most of the checks are only done when you instantiate the template. 大多数检查仅在您实例化模板时完成。

This is usually a good thing, as a code could work fine with one kind of template argument but fail to compile with another one. 这通常是一件好事,因为代码可以使用一种模板参数正常工作,但无法使用另一种模板参数进行编译。 If you have template overloading, the compiler will even ignore candidates that fail to compile, see SFINAE . 如果您有模板重载,编译器甚至会忽略无法编译的候选项,请参阅SFINAE

You do : 这样做

template <typename = void> void f() {
    return 0;
}

int main()
{
    f<int>();
}

prog.cpp: In function 'void f() [with = int]': prog.cpp:在函数'void f()[with = int]'中:
prog.cpp:7:12: instantiated from here prog.cpp:7:12:从这里实例化
prog.cpp:2:12: error: return-statement with a value, in function returning 'void' prog.cpp:2:12:错误:带有值的return语句,函数返回'void'

Though the program is still ill-formed , the compiler is choosing not to diagnose the semantic error (which is its prerogative) because you're never actually instantiating that function. 虽然程序仍然格式不正确 ,但编译器选择不诊断语义错误(这是它的特权)因为你实际上从未实际实例化该函数。

This is a quality of implementation issue. 这是一个实施质量问题。 The particular quote from the standard would be: 标准的特别引用是:

14.6/8 [...] If no valid specialization can be generated for a template definition, and that template is not instantiated, the template definition is ill-formed, no diagnostic required. 14.6 / 8 [...]如果无法为模板定义生成有效的专业化,并且未实例化该模板,则模板定义格式错误,无需诊断。 [...] [...]

That is, your program is ill formed because that template cannot be used to generate any valid specialization, but the compiler is not required to diagnose this. 也就是说,您的程序生成错误,因为该模板不能用于生成任何有效的特化 ,但编译器不需要诊断它。 When at a later time you instantiate the template, the compiler must generate the specialization, that specialization is not valid and the compiler complains. 当您稍后实例化模板时,编译器必须生成特化,专业化无效并且编译器会抱怨。

You don't get an error in the template definition because the compiler is following the no diagnostic required path, ie ignoring the problem until it can no longer ignore it in the instantiation. 您不会在模板定义中出现错误,因为编译器遵循无诊断所需的路径,即忽略问题,直到它在实例化中不再忽略它。

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

相关问题 为什么在使用重载赋值运算符时会出错,但我没有使用编译器提供的赋值运算符? - Why do I get errors when I use the overloaded assignment operator, but I don't get using the compiler-supplied one? 我不知道为什么这个 void function 给我一个错误 - I don't know why this void function is giving me an error 模板函数返回void - template function returning void 我不明白编译器错误 - I don't understand the compiler errors 我不明白为什么这个函数“从列表中返回一个指针” - I don't understand why this function “returns a pointer from the list” 为什么编译器不能从函数参数中推导出我的模板值? - Why can't the compiler deduce my template value from the function argument? C++ 如果我编写一个 function 模板并且不请求它的任何实例化,是否会从中生成任何模板函数? - C++ If I write a function template and don’t request any instantiations of it, will any template functions be generated from it? 在constexpr函数中返回C字符串:为什么编译器没有警告? - Returning a C string in a constexpr function: why no warning from the compiler? 从函数返回模板 - Returning Template from function 为什么编译器不能从返回类型中推导出模板参数? - Why can't the compiler deduce template parameter from return type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM