本文介绍了将源自元组的对象放入C ++中的向量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个有3个值的结构:一个字符串和两个整数。字符串是必须的,但是两个(或两个)int是可选的,如果没有指定可以默认为-1。

I want to create a struct with 3 values: a string and two ints. The string is mandatory, but either (or both) of the ints are optional and can default to -1 if not specified.

但是,认为我会尝试一个std ::元组。为了整合两个整数的可选性,我设置了一个Trio类继承自std :: tuple如下:

However, rather than use a struct, I thought I would try an std::tuple. In order to incorporate the optional-ness of the two ints, I setup a "Trio" class which inherits from std::tuple as below:

#include <string>
#include <tuple>

class Trio : public std::tuple<std::string, int, int>
{
    public:

    explicit Trio(std::string const & name, int val1 = -1, int val2 = -1)
    :
        tuple(name, val1, val2)
    {
    }
};

然后我通过将一些Trio对象推入std :: vector来测试Trio类: / p>

Then I go to test the Trio class by pushing some Trio objects into a std::vector:

#include <vector>
int main(void)
{
    std::vector<Trio> trios;

    Trio trio("trio1", 1, 1);
    trios.push_back(trio);

    return 0;
}

它在Visual Studio 2010中给出以下错误:

It gives me the following error in Visual Studio 2010:

>c:\program files (x86)\microsoft visual studio 10.0\vc\include\tuple(127):
    error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(const
    std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1
    from 'const Trio' to 'const std::basic_string<_Elem,_Traits,_Ax> &'
 with
 [
     _Elem=char,
     _Traits=std::char_traits<char>,
     _Ax=std::allocator<char>
 ]
 Reason: cannot convert from 'const Trio' to 'const
 std::basic_string<_Elem,_Traits,_Ax>'
 with
 [
     _Elem=char,
     _Traits=std::char_traits<char>,
     _Ax=std::allocator<char>
 ]
 No user-defined-conversion operator available that can perform
 this conversion, or the operator cannot be called

有人明白我在这里做错了什么吗?有什么明显的,我只是没有看到?

Does anyone understand what I am doing incorrectly here? Is there something obvious that I am just not seeing? Am I, perhaps, severely abusing the usage of std::tuple?

谢谢你,

Aaron

推荐答案

这是VC10的错误

VC10投诉,因为您的类不会 拥有一个复制构造函数。因此,为了复制类型 Trio 的值,它尝试将它们转换为 string ,这是什么构造函数你提供accept(其他参数可以给出默认值)。这是你遇到的错误:

VC10 complains because your class does not seem to have a copy constructor. Therefore, in order to copy values of type Trio, it tries to convert them into string, which is what the constructor you provide accepts (other arguments can be given default values). This is what the error you get complains about:

您可以通过明确添加一个复制构造函数来验证这是否正在发生,错误消失:

You can verify that this is indeed what is going on by adding a copy constructor explicitly and watch the error disappear:

Trio(Trio const& t) { *this = t; }



现在VC10感到满意,因为它看到一个复制构造函数,代码编译得很好。

Now VC10 is satisfied because it sees a copy constructor, and the code compiles fine.

然而,当用户没有显式提供复制构造函数时,你的编译器应该隐式生成一个。根据C ++ 11标准的第12.8 / 7段:

Nevertheless, when no copy constructor is explicitly provided by the user, your compiler should generate one implicitly. Per Paragraph 12.8/7 of the C++11 Standard:

Trio 没有显式声明任何副本构造函数。根据C ++ 11标准的12.8 / 2节,事实上:

Class Trio does not explicitly declare any copy constructor. According to Paragraph 12.8/2 of the C++11 Standard, in fact:

因此,您显式提供的构造函数不是复制构造函数,应该 禁止隐式生成复制构造函数。

Thus, the constructor you explicitly provide is not a copy constructor and should not inhibit the implicit generation of a copy constructor.

VC10可能会误解您提供的构造函数作为复制构造函数,因此,不会隐式生成一个。但是,由于上述内容,此行为是不正确的,并且符合错误。(*)

VC10 probably misinterprets the constructor you provide as a copy constructor and, therefore, does not generate one implicitly. Because of what written above, however, this behavior is a incorrect and qualifies as a bug.(*)

代码在Clang 3.2,GCC 4.7.2和ICC 13.0.1上编译良好。

As a side note, your code compiles fine on Clang 3.2, GCC 4.7.2, and ICC 13.0.1.

UPDATE:

我尝试用不涉及 std :: tuple<> 的更简单的数据结构重现问题,我失败了。因此,这个bug不仅仅是因为你的构造函数被VC10误解为一个显式拷贝构造函数。但是,这并不改变VC10的行为不正确的事实。

I tried to reproduce the problem with simpler data structures not involving std::tuple<>, and I failed. Therefore, the bug is not simply due to the fact that your constructor is misinterpreted by VC10 as an explicit copy constructor. However, this does not change the fact that VC10's behavior is incorrect.

这篇关于将源自元组的对象放入C ++中的向量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 09:06