English 中文(简体)
不可复制物体和价值初步化:g++ 和msvc
原标题:non-copyable objects and value initialization: g++ vs msvc

I m 看到在“g++”和“msvc”之间对非可扩展物体的初始价值存在某些不同的行为。 a. 属于不可扩展的类别:

class noncopyable_base
{
public:
    noncopyable_base() {}

private:
    noncopyable_base(const noncopyable_base &);
    noncopyable_base &operator=(const noncopyable_base &);
};

class noncopyable : private noncopyable_base
{
public:
    noncopyable() : x_(0) {}
    noncopyable(int x) : x_(x) {}

private:
    int x_;
};

以及使用价值初始化的模板,以便即使类型为分配,价值也会得到已知的价值:

template <class T>
void doit()
{
    T t = T();
    ...
}

并试图共同使用:

doit<noncopyable>();

截至VC++为止,这部工厂在武器上课。 9.0,但每版本g++都失败 我用(包括4.5.0版)测试了这一情况,因为复印件的构件是私人的。

两个问题:

  1. Which behavior is standards compliant?
  2. Any suggestion of how to work around this in gcc (and to be clear, changing that to T t; is not an acceptable solution as this breaks POD types).

P.S. I see the same problem with promotion:noncopyable.

最佳回答
问题回答

我不需要思考模板。 提 出

template <class T>
void doit()
{
    struct initer { T t; initer() : t() {} } inited;
    T& t = inited.t;
    ...
}

第12.8/14节:

如果一物体的影印机或复印机投递操作者被含蓄地使用,且该成员的职能无法进入,则该方案便不知情。

然后,第12.8/15节:

如果符合某些标准,即使该物体的造影机和(或)脱轨机具有副作用,也允许执行该规定。

因此,如果执行没有向影印施工商发出(显然允许这样做)的电话,那么问题究竟是影印人是否实际使用<>><>>>?

而且,根据3.2/2节,答案是:

即使实际使用该电话,也使用复印机。

你们是否看到了在利用MSVC/Wall编辑时发生的情况? 声明如下:

nocopy.cc(21) : warning C4625:  noncopyable  : copy constructor could not be
generated because a base class copy constructor is inaccessible
nocopy.cc(21) : warning C4626:  noncopyable  : assignment operator could not be
generated because a base class assignment operator is inaccessible

GCC remedy: create a copy constructor for noncopyable (and an assignment operator ideally!) that does what it can to copy the information from noncopyable_base, namely invoking the constructor for noncopyable_base that has no parameters (since that is the only one accessible by noncopyable) and then copying any data from noncopyable_base. Given the definition of noncopyable_base, however, it seems there is no data to copy, so the simple addition of noncopyable_base() to the initializer list of a new noncopyable(const noncopyable &) function should work.

注意劳动和社会福利部对你的方案所说的话。 还注意到如果你使用<代码>T t(>,而不是T t = T(,,则由海事安全委员会提出,尽管海合会习惯在没有任何警告的情况下接受。





相关问题
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?

热门标签