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

  • Home
  • SEARCH
  • 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 8892665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:03:48+00:00 2026-06-14T23:03:48+00:00

Consider the following code: #include <iostream> template<class T> void f(T& t) { t =

  • 0

Consider the following code:

#include <iostream>

template<class T>
void f(T& t)
{
    t = T();
}

int main()
{
    int x = 42;
    f(x);
    std::cout << x;
}

Does the C++11 standard define what the output shall be? My compiler outputs 0, however I was under the impression the default constructor of a primitive type is a null operation or undefined behaviour.

  • 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-14T23:03:50+00:00Added an answer on June 14, 2026 at 11:03 pm

    There’s no “default constructor” involved in your code. Only class types can have constructors. Scalar types have no constructors, default or otherwise.

    The T() syntax creates a temporary object initialized by so called value-initialization. Value-initialization resolves to constructor call only for class types, and only for those with user-defined constructors (with some nuances in C++11). For other types value-initialization does not involve any constructors at all. It proceeds in accordance with its own specific and rather elaborate initialization rules that define the initial value of the data directly, without involving any constructors (see 8.5 in the language specification).

    For scalar types value-initialization performs zero-initialization. This is why your code is guaranteed to output zero. The exact specifics of the abstract initialization process changed between the versions of C++ language standard, however since the beginning of times C++ language guaranteed that T() expression for T == int evaluated to zero. I.e. even in C++98 your code will output zero.

    It is a common misconception that all these T(...) expressions somehow necessarily imply constructor calls. In reality, T(...) expression is a functional cast expression (regardless of the number of arguments) (see 5.2.3 in the language specification), which might resolve to constructor call in some narrow set of specific situations and has nothing to do with any constructors in other situations.

    For example, this code

    struct S { int x, y; };
    
    S s = S();
    

    is guaranteed to initialize s with zeros (both s.x and s.y) despite that fact that class S has a default constructor. I brought up this example specifically to illustrate the fact that even in situations when the default constructor exists, the T() expression can still completely ignore it and work by its own rules instead.

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

Sidebar

Related Questions

Consider the following code: #include <iostream> #include <functional> using namespace std; template<class T> void
Consider the following code: #include <iostream> using namespace std; int main() { int x,
Consider the following code: #include<iostream> #include<vector> using namespace std; class Foo { public: template<
Consider the following code: #include <iostream> using namespace std; class Test { static int
Consider the following code: #include <iostream> #include <cinttypes> template<class T> void f(); template<> inline
Consider the following code: #include<iostream> using namespace std; class sample { int a; int
please consider following code #include <iostream> using namespace std; class Digit { private: int
Consider the following code: #include <iostream> #include <cstdio> #include <cstring> using namespace std; template<class
Consider the following code: #include <iostream> #include <vector> #include <boost/numeric/ublas/vector.hpp> #include <boost/numeric/ublas/io.hpp> int main()
Consider the following piece of code: #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void)

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.