Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7953 Discussions

ICC 16.0.2 incorrect error on anonymous union initialization

timmie_s_
Beginner
746 Views

Hello,

  I've run into a compile error with icpc 16.0.2 that I think is incorrect.  If I have an anonymous union in a class the compiler only considers it intialized if I initialize the first data member of the union.  That's too strict.  The test case below demonstrates the error.

 

// Test file demostrating what I think is an invalid compile error.
//
//> icpc --version
//icpc (ICC) 16.0.2 20160204
//Copyright (C) 1985-2016 Intel Corporation.  All rights reserved.
//
//> icpc -std=c++11 bar.cc
//bar.cc(20): error: constexpr constructor fails to initialize an anonymous union (defined at line 7)
//                { }
//                    ^

class data_type
{
private:
  // Reordering the data members so that m_index is first allows the
  // constructor to initialize m_index without error, but then a constructor
  // initializing m_data would have the same error.
  union
  {
    void*  m_data;
    int    m_index;
  };
  int  m_epoch;

public:
  constexpr data_type(void) noexcept
    :
    m_index(99), // this initialization results in error
//      m_data(nullptr), // this initialization allows compilation to succeed
      m_epoch(99)
  { }
};

int main()
{
  data_type d;
}

0 Kudos
4 Replies
KitturGanesh
Employee
746 Views

Hi Timmie,
Thanks, this is a bug and I'll file the issue with the developers and will keep you updated on the progress of  the issue as well, appreciate much.

Kittur

 

0 Kudos
KitturGanesh
Employee
746 Views

HI Timmie,
The issue is already being worked on by the product team and I'll let you know as soon as the release is out. Thanks for catching this and the obvious workaround is to initialize the first field instead, until the release with the fix is out - appreciate much.

Regards,
Kittur

0 Kudos
timmie_s_
Beginner
746 Views

Thank you for the quick response and confirmation of the bug.

The workaround we employed in our code was to ifdef the constructor with __INTEL_COMPILER and if the intel compiler is being used we remove the constexpr qualifier from the constructor.  The reason for this approach is that one of our classes has two constexpr constructors, each initializing a different field of the union, so no ordering of the union members was satisfactory.

I look forward to when the release with the bug fix is available.

Thanks again,
Timmie

0 Kudos
KitturGanesh
Employee
746 Views

Got it, thanks for letting me know Timmie.  Sure, I'll update you as soon as the release with the fix is out. Appreciate your patience again, till then.

Regards,
Kittur

0 Kudos
Reply