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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:15:23+00:00 2026-06-10T20:15:23+00:00

Why can’t I initialize a value with auto and pass it into a function

  • 0

Why can’t I initialize a value with auto and pass it into a function that expects a decltype as a parameter?

Let me set the scene, and show you a tiny program.


Here is a function that returns a value.

int Function(void);

In this case, it happens to be an integer, but the return type is subject to change.
That is why this next function, is written as followed:

void What_I_Take_Depends_On_Function(decltype(Function()) x);

If someone decides to change the return type of Function, then the deceleration of this function will not need to be changed. Yes, the definition of the function may not handle the new type correctly, or if Function’s return type is changed to void there will be an issue, but that is irrelevant to my problem.

#include <iostream>
#include <cstdlib>

int Function(void){return 5;}

void What_I_Take_Depends_On_Function(decltype(Function()) x){return;}

int main(){

    //assignments(Edit: these are initializations)
    int  var1 = Function();
    auto var2 = Function();

    //initializations
    int  var3 {Function()};
    auto var4 {Function()};

    What_I_Take_Depends_On_Function(var1); //works
    What_I_Take_Depends_On_Function(var2); //works
    What_I_Take_Depends_On_Function(var3); //works
    What_I_Take_Depends_On_Function(var4); //COMPILER ERROR

    //cannot convert ‘std::initializer_list<int>’ to ‘int’ for argument ‘1’ to ‘void What_I_Take_Depends_On_Function(int)’

    return EXIT_SUCCESS;
}

So why is var4 an initializer_list and not an int?
Can’t auto just deduce that Function is going to return an int,
and then change the deceleration to that similar of var3?

  • 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-10T20:15:24+00:00Added an answer on June 10, 2026 at 8:15 pm

    The brace-init-list {...} is the new uniform initialization syntax for C++11, and it can be used to initialise any automatic, static or member variable if the type of that variable is known. These are a few examples of valid C++11 initialization:

    class S
    {
    private:
      int _m;
    public:
      S(int m) : _m { m }  // <== initialization of a member
      {}
    };
    
    
    int main() {
    
      constexpr unsigned i { 10 }; // <== initialization of a constexpr
    
      S s { i };  // <== initialization by calling constructor
    
      /* ... */
    }
    

    In none of the above cases will a std::initializer_list be generated. The brace-init-list {...} simply means the compiler will try to identify a suitable constructor for the given data type, whose argument list matches the contents of the brace-init-list (§8.5.4).

    There are two special cases:

    1. If one of the constructors defined for the given data type takes a std::initializer_list<T> as argument, and the contents of the brace-init-list are all implicitly convertible to T. This is the case when you use one of the built-in container types, e.g.

      std::vector<int> vec { 1, 2, 3 };
      

      std::vector<T> has a constructor std::vector<T>(const std::initializer_list<T>), so the brace-init-list will be converted to a std::initializer_list<int> and as such copied into the vector constructor. The constructor will iterate through the list and append the elements one by one.

      This is a bit of a trap when the list contains only one argument:

      std::vector<int> vec { 10 };
      

      Here, the same happens, so you will get a vector that contains the one element 10. This is different from using old-style syntax:

      std::vector<int> vec(10);
      

      This calls the std::vector<int>(const size_t) constructor, i.e. it creates a vector of 10 default-initialised elements.

    2. If the type of the variable to be initialised is not pre-determined, i.e. when auto is used in the declaration:

      auto v { 1, 2, 3 };
      

      In this case (§7.1.6.4/6) the compiler cannot identify a suitable constructor, because any data type that takes three integer (or convertible-to-integer) arguments is a possible candidate. The rule here is that the compiler assumes std::initializer_list<int> as the data type for v. That is what happens in your case as well.

    In other words, using brace-init-lists is fine (and even encouraged) for initialization, but you can’t readily combine it with auto. To solve you problem, you need to either declare the data type explicitly

    int var4 { Function() };
    

    or, to keep things flexible, use decltype here too:

    decltype(Function()) var4 { Function() };
    

    Alternatively, you can use old-style syntax:

    auto v (Function());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone let me know how can we change the value of kendo combobox
Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can I be sure about the order in a Python dictionary? The function op.GetTangent(id)
Can we change the color of the divider? Apple documentations says, that we can
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Can anyone help me with an excel formula that groups related rows and creates
Can functors be defined locally within the body of a function? One possible use
Can somebody point me to a resource that explains how to go about having
Can a LINQ enabled app run on a machine that only has the .NET
Can you have submenus with the top level set to checkable in WPF? I

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.