Why can't I initialize a value using braces with auto and pass it into this function

南楼画角 提交于 2019-12-24 11:30:04

问题


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:


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());



回答2:


No, auto will not "just deduce that Function is going to return an int"; the way how the auto specifier works is well-defined in 7.1.6.4, and is as follows:

(7.1.6.4/p6) Once the type of a declarator-id has been determined according to 8.3, the type of the declared variable using the declarator-id is determined from the type of its initializer using the rules for template argument deduction. Let T be the type that has been determined for a variable identifier d. Obtain P from T by replacing the occurrences of auto with either a new invented type template parameter U or, if the initializer is a braced-init-list (8.5.4), with std::initializer_list<U>. The type deduced for the variable d is then the deduced A determined using the rules of template argument deduction from a function call (14.8.2.1), where P is a function template parameter type and the initializer for d is the corresponding argument. If the deduction fails, the declaration is ill-formed.




回答3:


I had a misunderstanding of syntax.
I was accustomed to initializing variables like this:

int i{0};

However, I need to initalize this type with parenthesis not brackets.
Thank you Raymond



来源:https://stackoverflow.com/questions/12224733/why-cant-i-initialize-a-value-using-braces-with-auto-and-pass-it-into-this-func

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!