Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C++: 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. 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 <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. Why?

3 Answers

Relevance
  • oops
    Lv 6
    9 years ago
    Favorite Answer

    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<typename T>

        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.

  • 9 years ago

    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
    Lv 4
    9 years ago

    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;

    }

Still have questions? Get your answers by asking now.