Question:
C++: Why don't I get compiler errors from returning from a void function template?
David
2012-11-11 05:31:53 UTC
Consider:

    void f() {
        return 5;
    }

The above will raise errors. But why not this?:

    template void f() {
        return 0;
    }

I'm compiling with 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 (i.e f()) without getting:

    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?

Here is the code I have:

    template void f() {
        return 0;
    }

    // pass

    int main() {



    }

The above code will pass despite a presumably illegal return statement in a function returning void. Why?
Three answers:
oops
2012-11-11 05:59:39 UTC
Simply, templates are not compiled until they are instantiated. The compiler doesn't complain about your code, because the code does not get compiled. Well, that's not completely true. It does get partially compiled. It gets checked for correct syntax, but that's it. Since 'return 0;` is valid syntax, it passes. It doesn't do the extra work of validating that the value you returned matches the return type of the function until you actually try to instantiate the template.



Why doesn't the compiler check it until it is instantiated? Well, it is often the case that it can't. It is often the case that it needs to know the template arguments in order to determine if the value returned is of the correct return type. As a very simple example:



    template

    T Twice(T val)

    {

        return val + val;

    }



The compiler cannot know if the result of the operation 'T + T' actually results in a value that is either of type T, or implicitly convertible to type T. Not until it knows what T is, which it only knows if the template is instantiated.



This is not a problem with your function, but why make the compiler's job harder by making a special case? It's much simpler to say "don't bother compiling templates until they are instantiated". And that rule is not a problem for most people. If there is a problem in a template function that isn't detected because the template function is never instantiated, it's not really much of a problem. The moment someone tries to instantiate it, the compiler will immediately show them the problem, which will be very easy to fix. The fix won't impact any existing code, since this template was never used in any existing code.
Damianek
2012-11-11 13:44:32 UTC
Because since you said void, it just doesn't return 5, it return void. It's based on the declaration, if you said long int but you tried to return an integer it would turn that into a long.
T
2012-11-11 13:45:55 UTC
you can't return any value in a void function

you can't say return 0; in a void function

if you want a void function to return you values you should declare the variables as public variables,

and in the void function you will be able to change their values, example



public int a=0;



void function(int b){

a = b;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...