引用的均匀初始化 [英] Uniform initialization of references

查看:119
本文介绍了引用的均匀初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正试图理解新的统一初始化C ++ 0x。不幸的是,我使用引用的统一初始化扼杀了。示例:

  int main(){
int a;
int& ref {a};
}

此示例工作正常:

 %LANG = C g ++ uniform_init_of_ref.cpp -std = c ++ 0x -o uni -Wall -Wextra 
uniform_init_of_ref.cpp:在函数`int main ':
uniform_init_of_ref.cpp:3:10:warning:unused variable`ref'[-Wunused-variable]


b $ b

更新 Comeau会抛出该示例的错误,因此gcc也不应该编译它)



,如果我使用自定义数据类型而不是整数,则它不再工作:

  class Y 
{};

int main()
{
Y y;
Y& ref {y};
}

%LANG = C g ++ initialization.cpp -std = c ++ 0x -o初始化-Wall -Wextra
initialization.cpp:在函数`int main() ':
initialization.cpp:9:13:error:从<大括号初始化程序列表>'
初始化的右值初始化类型`Y&'的非常量引用。不幸的是,不幸的是,我们不知道如何使用这个变量,我没有在标准草案中找到相关部分。我的猜想是,我误解统一初始化的使用,因为Comeau抱怨这个消息:

  ComeauTest.c :error:引用变量ref需要初始化器
Y& ref {y};

那么,有人能指出我的方向吗? / p>




如果你想知道为什么这个问题是相关的,为什么我不只是使用 Y& ref(y):我想在构造函数的初始化列表中使用统一初始化:

  class X {}; 

class Y {
const X& X;

public:
Y(const X& xx):
x {xx}
{}
}

int main(){
X x;
Y y {x};
}

这会失败,并显示与上述相同的错误讯息。



注意:




  • 我使用 LANG = C 启用英语错误讯息。

  • gcc版本:4.6.1


方案

根据 N2672 段落8.5.4.4应该说:


否则,如果T是引用类型,T引用的类型的临时值为list-初始化,并且引用绑定到该临时。 [注意:像往常一样,如果引用类型是非const类型的左值引用,则绑定将失败,程序不成形。 ]


这意味着引用的统一初始化将它们绑定到新的匿名实例,所以在我看来相当无用。这仍然不能解释为什么一个工作,另一个不工作;它们应该表现相同(除非 Y 有一些显式构造函数)。


I am currently trying to understand the new uniform initialization of C++0x. Unfortunately, I stumpled over using uniform initialization of references. Example:

int main() {
   int a;
   int &ref{a};
}

This example works fine:

% LANG=C g++ uniform_init_of_ref.cpp -std=c++0x -o uni -Wall -Wextra
uniform_init_of_ref.cpp: In function `int main()':
uniform_init_of_ref.cpp:3:10: warning: unused variable `ref' [-Wunused-variable]

(Update Comeau throws an error for that example, so maybe gcc shouldn't compile it as well)

Now, if I use a custom data type instead of an integer, it doesn't work anymore:

class Y
{};

int main()
{
    Y y;
    Y &ref{y};
}

% LANG=C g++ initialization.cpp -std=c++0x -o initialization -Wall -Wextra
initialization.cpp: In function `int main()':
initialization.cpp:9:13: error: invalid initialization of non-const reference of type `Y&' from an rvalue of type `<brace-enclosed initializer list>'
initialization.cpp:9:8: warning: unused variable `ref' [-Wunused-variable]

Unfortunately, I didn't find the relevant section in the standard draft. My guess is that I am misunderstanding the usage of uniform initialization, as Comeau complains with this message:

ComeauTest.c(9): error: reference variable "ref" requires an initializer
      Y &ref{y};

So, can someone of you point me in the right direction?


In case that you want to know why this question is relevant and why I don't just use Y &ref(y): I'd like to be able to use uniform initialization in the initialization list of a constructor:

class X { };

class Y {
    const X& x;

    public:
        Y (const X& xx):
            x{xx}
        {}
};

int main () {
    X x;
    Y y{x};
}

This fails with the same error message as above.

Note:

  • I am using LANG=C to enable english error messages.
  • gcc version: 4.6.1

解决方案

According to N2672 the paragraph 8.5.4.4 should say:

Otherwise, if T is a reference type, an rvalue temporary of the type referenced by T is list-initialized, and the reference is bound to that temporary. [ Note: As usual, the binding will fail and the program is ill-formed if the reference type is an lvalue reference to a non-const type. ]

which (if I understand it correctly) means uniform initialization of references binds them to new anonymous instances, so it seems to me it's pretty useless. That still does not explain why one works and the other does not; they should behave the same (unless Y has some explicit constructors).

这篇关于引用的均匀初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆