Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8790389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:38:15+00:00 2026-06-13T22:38:15+00:00

Consider: void f() { return 5; } The above will raise errors. But why

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:38:17+00:00Added an answer on June 13, 2026 at 10:38 pm

    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. […]

    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, i.e. ignoring the problem until it can no longer ignore it in the instantiation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code: int main() { int e; prn(e); return 0; } void prn(double
Consider the following code: struct Undefined; template <typename T> void TemplateFunction() { Undefined obj;
template <typename T> void f() { return 0; // returning value from function returning
Consider the following program: #include <stdio.h> int main(void) { return 0; } When i
Consider this code: void res(int a,int n) { printf(%d %d, ,a,n); } void main(void)
consider following: 1st APPROACH: public void f3() { f2(); f1(); } and this ...
Consider the following code /* * To change this template, choose Tools | Templates
Consider the following snippet: void Foo() { // ... } void Bar() { return
Consider the following: public static class FileSerializer { public static void SaveToFile<T>(this T obj,
Consider the following example template<class Type = void> class MyClass { public: double getValue()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.