gpt4 book ai didi

c++ - 初始化列表的赋值

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:23:00 27 4
gpt4 key购买 nike

下面的代码是我的问题的一个最小示例。我创建了一个包含固定大小数组的简单模板类,并重载了赋值运算符以接受任何定义方法的类 size()begin() (例如,initializer_list 秒)。我不明白为什么 g++ 无法解析我对该运算符的调用(我使用的是 gcc 4.6):

***.cpp: In function ‘int main()’:
***.cpp:46:22: error: no match for ‘operator=’ in ‘a = {42, -1.0e+0, 3.14158999999999988261834005243144929409027099609375e+0}’
***.cpp:46:22: note: candidates are:
***.cpp:23:8: note: template<class U> A<T, N>::self& A::operator=(const U&) [with U = U, T = double, unsigned int N = 3u, A<T, N>::self = A<double, 3u>]
***.cpp:8:7: note: A<double, 3u>& A<double, 3u>::operator=(const A<double, 3u>&)
***.cpp:8:7: note: no known conversion for argument 1 from ‘<brace-enclosed initialiser list>’ to ‘const A<double, 3u>&’
***.cpp:8:7: note: A<double, 3u>& A<double, 3u>::operator=(A<double, 3u>&&)
***.cpp:8:7: note: no known conversion for argument 1 from ‘<brace-enclosed initialiser list>’ to ‘A<double, 3u>&&’

第一个候选者被正确列出,但没有相关的错误消息。这是代码:

#include <iostream>
#include <algorithm>
#include <initializer_list>

// ------------------------------------------------------------------------

template <typename T, unsigned N>
class A
{
public:

typedef A<T,N> self;

// Default ctor
A() {}

// Copy ctor
template <typename U>
A( const U& other ) { operator=(other); }

// Assignemnt
template <typename U>
self& operator= ( const U& other )
{
if ( other.size() == N )
std::copy_n( other.begin(), N, m_data );
return *this;
}

// Display contents
void print() const
{
for ( unsigned i = 0; i < N; ++i )
std::cout << m_data[i] << " ";
std::cout << std::endl;
}

private:
T m_data[N];
};

// ------------------------------------------------------------------------

int main()
{
A<double,3> a;
a = {42,-1.0,3.14159};
a.print();
}

有谁知道为什么这可能是模棱两可的,或者我做错了什么?


注意:理想情况下,我什至想用一个 A<double,3> a = {42,-1.0,3.14159}; 替换 main 的前两行但我不确定如何,我目前收到以下错误:

***: In function ‘int main()’:
***:45:34: error: could not convert ‘{42, -1.0e+0, 3.14158999999999988261834005243144929409027099609375e+0}’ from ‘<brace-enclosed initialiser list>’ to ‘A<double, 3u>’

最佳答案

不同于auto ,其中大括号初始化列表被推断为 initializer_list , 模板参数推导将其视为非推导上下文,除非存在类型为 initializer_list<T> 的相应参数, 在这种情况下 T可以推导出来。

来自 §14.8.2.1/1 [temp.deduct.call](强调已添加)

Template argument deduction is done by comparing each function template parameter type (call it P) with the type of the corresponding argument of the call (call it A) as described below. If removing references and cv-qualifiers from P gives std::initializer_list<P0> for some P0 and the argument is an initializer list (8.5.4), then deduction is performed instead for each element of the initializer list, taking P0 as a function template parameter type and the initializer element as its argument. Otherwise, an initializer list argument causes the parameter to be considered a non-deduced context (14.8.2.5).

因此您的 operator= 的参数未被推断为 initializer_list<double> .要使代码正常工作,您必须定义一个 operator=这需要 initializer_list争论。

template <typename U>
self& operator= ( const std::initializer_list<T>& other )
{
if ( other.size() == N )
std::copy_n( other.begin(), N, m_data );
return *this;
}

关于c++ - 初始化列表的赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23186775/

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