gpt4 book ai didi

c++ - 在模板函数中使用 initializer_list

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:29:36 26 4
gpt4 key购买 nike

我正在尝试使用函数模板 foo 将参数转换为 initializer_list。但是,它转换的 initializer_list 具有与输入参数不同的奇怪值。

#include <iostream>
#include <iterator>
#include <string>
#include <vector>

using namespace std;

template<class T>
void func(std::initializer_list<T> a_args)
{
if (a_args.begin() != a_args.end())
{
auto last = prev(a_args.end());
copy(a_args.begin(), last, ostream_iterator<int>(cout, ","));
cout << *last;
}
cout << endl;
}

template<class T, class ...Args>
struct first_of
{
typedef T type;
};

template<class ...Args>
initializer_list<typename first_of<Args...>::type> foo(Args&&... args)
{
return { forward<Args>(args)... };
}

int main()
{
func({1,2,3});
auto x = foo(1,2,3);
func(x); //this should be the same as func({1,2,3}) but not.
}

LIVE CODE

输出如下:

1,2,3
-326483696,32767,0

这里有什么问题吗?

最佳答案

std::initializer_list<T>应该只用作临时对象或函数参数,因为它指的是临时数组。

8.5.4/5-6:

An object of type std::initializer_list<E> is constructed from an initializer list as if the implementation allocated a temporary array of N elements of type const E, where N is the number of elements in the initializer list. ...

The array has the same lifetime as any other temporary object (12.2), except that initializing an initializer_list object from the array extends the lifetime of the array exactly like binding a reference to a temporary.

18.9/2:

An object of type initializer_list<E> provides access to an array of objects of type const E. [Note: A pair of pointers or a pointer plus a length would be obvious representations for initializer_list. initializer_list is used to implement initializer lists as specified in 8.5.4. Copying an initializer list does not copy the underlying elements.]

因此返回一个 initializer_list对象和以下一样糟糕:

struct int_ref {
int& ref;
explicit constexpr int_ref(int& r) : ref(r) {}
};

int_ref func() {
int n = 5;
return int_ref(n);
}

关于c++ - 在模板函数中使用 initializer_list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19066974/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com