6

Please consider the example below. The class template Sample attempts to initialize reference members using a member function of a template parameter, expected to return an appropriate reference.

class Inner
{
public:
    Inner() : x_{1}
    {
    }

private:
    int x_;
};

class Outer
{
public:
    Inner& GetInner()
    {
        return inner_;
    }

private:
    Inner inner_;
};

template<typename T>
class Sample
{
public:
    Sample(T& outer) :
        innerOk_{static_cast<Inner&>(outer.GetInner())},
        innerFail_{outer.GetInner()} // ICC fails with "error: initial value of reference to non-const must be an lvalue"
    {
    }

private:
    Inner& innerOk_;
    Inner& innerFail_;
};

int main(int argc, char* argv[])
{
    Outer outer;
    Sample<Outer> s{outer};
    return 0;
}

Of course, before seeing an actual parameter for T, there is no way for a compiler to tell whether the initialization is valid, or whether the type returned by the function would not be suitable. As it also couldn't tell whether T is going to have such a function or whether it's going to return anything at all.

Following workarounds make the code pass:

  • Use a static_cast to confirm the expected type, as with innerOk_ in the example.
  • Use ()-initialization instead of {}.

Is ICC (19.0.1) right in performing that check so early and rejecting the direct initialization? GCC accepts both.

Evg
  • 25,259
  • 5
  • 41
  • 83
  • 3
    Looks like an ICC bug. – Evg Nov 06 '19 at 10:06
  • 4
    Welcome to Stack Overflow! It's great that you have provided a [mre]. You can take the [tour] for an overview of our site. The code [compiles fine](https://godbolt.org/z/8vaCsU) on the three major implementations, so probably an ICC bug. – L. F. Nov 06 '19 at 10:07
  • 1
    I've also checked the older versions of ICC on compiler explorer, seems to be a problem everywhere. Best to report this on the forum (if you use the free version) or on the bug tracker (if you have a paying version), see https://software.intel.com/en-us/forums/intel-c-compiler/topic/753992 for details. – JVApen Nov 06 '19 at 10:18

0 Answers0