English 中文(简体)
复印机 需要温器
原标题:Copy Constructor Needed with temp object

只有在有影印机时,方可使用以下代码。

当我添加印刷说明(通过<代码>std:cout)并且提供复印件时,不使用(我假定,有这么多的编辑来删除不必要的复印件)。

但在产出<代码>operator <<和功能plop(<>plop(><>>>/code>(在我制造临时物体时)中,我看不出需要复印件。 有些人可以解释,为什么在我通过提及一切时,语言需要它(或者我做错的话)。

#include <iostream>

class N
{
    public:
        N(int)  {}
    private:
        N(N const&);
};

std::ostream& operator<<(std::ostream& str,N const& data)
{
    return str << "N
";
}

void plop(std::ostream& str,N const& data)
{
    str << "N
";
}

int main()
{
    std::cout << N(1);     // Needs copy constructor  (line 25)
    plop(std::cout,N(1));  // Needs copy constructor

    N    a(5);
    std::cout << a;
    plop(std::cout,a);
}

Compiler:

[Alpha:~/X] myork% g++ -v
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5646~6/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5646)

[Alpha:~/X] myork% g++ t.cpp
t.cpp: In function ‘int main()’:
t.cpp:10: error: ‘N::N(const N&)’ is private
t.cpp:25: error: within this context
t.cpp:10: error: ‘N::N(const N&)’ is private
t.cpp:26: error: within this context

This is a simplified version of some real code.
In the real code I have a class that contains a std::auto_ptr. This means that a copy constructor that takes a const reference is not valid (without some work) and I was getting an error indicating that the copy constructor was not available because of it:

改变这一类别:

class N
{
    public:
        N(int)  {}
    private:
        std::auto_ptr<int>  data;
};

然后是:

t.cpp:25:差错:没有“N:N(N)”的配对功能

最佳回答

http://gcc.gnu.org/gcc-3.4/changes.html“rel=“noreferer” http://gcc.gnu.org/gcc-3.4/changes.html

When binding an rvalue of class type to a reference, the copy constructor of the class must be accessible. For instance, consider the following code:

class A 
{
public:
  A();

private:
  A(const A&);   // private copy ctor
};

A makeA(void);
void foo(const A&);

void bar(void)
{
  foo(A());       // error, copy ctor is not accessible
  foo(makeA());   // error, copy ctor is not accessible

  A a1;
  foo(a1);        // OK, a1 is a lvalue
}

This might be surprising at first sight, especially since most popular compilers do not correctly implement this rule (further details).

http://www. open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#391”rel=“noreferer”

问题回答

标准中的可适用部分是第8.5.3/5节,该节涉及参考文献的初始化和第3.10/6节,其中说明了什么是高价值和什么是价值(在C++中并不总是显而易见的)。

在此情况下,您的初始表达是:“N(1)”,因此,您明确提出使用功能性表示的物体。 根据3.10/6号文件,这一表述是巨大的。

然后,我们必须通过8.5.3/5号文件中的规则走下去,并使用第一个适用的规则。 第一个可能性是,该表述代表了价值,或者可以暗中转换为价值。 你的表达是一种高价值,默示转换为低价值需要一种转换功能,即回归参考,在这种情况下似乎不存在这种参照,因此似乎不适用。

下一个规则指出,必须提及一个星座(这里就是这种情况)。 在此情况下,该表述是类的超值,与参考(即指同一类或该类的基数)相符。 这意味着第151页底部的子弹(C++ 2003 PDF)似乎适用。 在此情况下,汇编者可以将提及直接与代表超值的标语联系起来,或制作临时版面值,并对该临时复印件具有约束力。

但是,无论是哪种方式,标准都明确要求:“将用来复制件的施工人,不论是否实际复制,都应可以打电话”。

因此,I believe:gcc是发出错误信息的权利,而其他则在技术上是错误的接受该守则。 我将你的法典简化如下:

class N {
    public:
        N(int)  {}
    private:
        N(N const&);
};

void plop(N const& data) { }

int main() {
    plop(N(1));
}

当使用“-A”(严重错误模式)时,Comeau给出以下错误信息:

"plop.cpp", line 12: error: "N::N(const N &)", required for copy that was
          eliminated, is inaccessible
      plop(N(1));
           ^

同样,在使用“Za”(其“ANSI”模式)时,VC++ 9 表示:

plop.cpp
plop.cpp(12) : error C2248:  N::N  : cannot access private member declared in class  N 
        plop.cpp(6) : see declaration of  N::N 
        plop.cpp(2) : see declaration of  N 
        while checking that elided copy-constructor  N::N(const N &)  is callable
        plop.cpp(6) : see declaration of  N::N 
        when converting from  N  to  const N & 

我的猜测是,其他大多数汇编者大致相同。 由于它们优化了向建筑商发出的呼吁,它们通常并不要求存在或可以进入。 当你要求他们尽可能准确地遵守标准时,他们会发出错误信息,因为即使他们不使用标准,在技术上也是必要的。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?