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 4056148
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:47:25+00:00 2026-05-20T14:47:25+00:00

What if I write return statement in constructor? Is it standard conformant? struct A

  • 0

What if I write return statement in constructor? Is it standard conformant?

struct A
{ 
     A() { return; } 
};

The above code compiles fine, without any error at ideone. But the following code doesn’t:

struct A
{ 
   A() { return 100; } 
};

It gives this error at ideone:

error: returning a value from a constructor

I understand that returning value from constructor doesn’t make sense at all, because it doesn’t explicitly mention return type, and we cannot store the returned value after all. But I’m curious to know :

  • Which statement from the C++ Standard allows the first example but forbids the second one? Is there any explicit statement?
  • Is the return type in the first example void?
  • Is there any implicit return type at all?
  • 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-05-20T14:47:26+00:00Added an answer on May 20, 2026 at 2:47 pm

    Yes, using return statements in constructors is perfectly standard.

    Constructors are functions that do not return a value. The family of functions that do not return a value consists of: void functions, constructors and destructors. It is stated in 6.6.3/2 in the C++ standard. The very same 6.6.3/2 states that it is illegal to use return with an argument in a function that does not return a value.

    6.6.3 The return statement

    2 A return statement without an
    expression can be used only in
    functions that do not return a value,
    that is, a function with the return
    type void, a constructor (12.1), or a
    destructor (12.4). A return statement
    with an expression of non-void type
    can be used only in functions
    returning a value; the value of the
    expression is returned to the caller
    of the function.

    Additionally, 12.1/12 states that

    12.1 Constructors

    12 No return type (not even void) shall
    be specified for a constructor. A
    return statement in the body of a
    constructor shall not specify a return
    value.

    Note, BTW, that in C++ it is legal to use return with an argument in a void function, as long as the argument of return has type void

    void foo() {
      return (void) 0; // Legal in C++ (but not in C)
    }
    

    This is not allowed in constructors though, since constructors are not void functions.

    There’s also one relatively obscure restriction relevant to the usage of return with constructors: it is illegal to use return in function-try-block of a constructor (with other functions it is OK)

    15.3 Handling an exception

    15 If a return statement appears in a
    handler of the function-try-block of a
    constructor, the program is ill formed.

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

Sidebar

Related Questions

i am trying to write one function which return sql statement like function get_sql($name=0,$date_start=0,$date_end=0)
I want to write a mysql statement which will return a list of results
I need to write a case statement that will return 1 of 3 if
I am trying to write some code to return an array in C#, but
I am getting an error about the return statement (or cast) in the final
I have below code. if query is executed without exception then true should return,
I'm working on a C-brain teaser: Write the standard Hello-World program, without semi-colons. My
I'm trying to write an sql statement that will return a question and all
How to write LINQ statement that returns ROWNUMBER of element with id == something?
Generally speaking, the SQL queries that I write return unformatted data and I leave

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.