[cfe-users] Warning: binding reference to temp value

Peter Caspers pcaspers1973 at gmail.com
Mon Aug 19 14:44:37 PDT 2013


Hi all,

I am wondering whether in the following situation a warning would be
useful: Suppose U any class and X defined as follows

class X {
public:
    X(const U& u = U()) : u_(u), u2_(U()) {}
    const U &u_, &u2_;
};

then compiling with

clang++ -Weverything x.cpp

gives the following warning for u2_

x.cpp:50:38: warning: binding reference member 'u2_' to a temporary
value [-Wdangling-field]
    X(const U& u = U()) : u_(u), u2_(U()) {}

but exactly the same problem occurs for u_ when u is given the default
value U() in the constructor (i.e. if the constructor is called
without parameters like e.g. in "X x;").

Wouldn't a similar warning as given for u2_ be nice for u_ also or is
there a specific reason for not generating such a warning ?

Thank you
    Peter



More information about the cfe-users mailing list