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

Bug with string constructor in nested structs

sbolding
Beginner
1,392 Views

Hello,

Below is a reproducer for an internal compiler error we hit with icpc 19.0.4 (with gcc 7.4 for STL, but that doesn't seem to have an effect):  

#include <string>
#include <map>

struct StringMember {
  std::string a = "";
  // std::string a {}; //This works fine.
};

struct C {
  StringMember mem = {};
};

struct B {
  std::map<int, C> map_ {};
};

int main () {
  return 1;
}

I tested on god bolt with icc 21.1.9 and it had the same behavior:  https://godbolt.org/z/ePbf94 .  Once we found the cause of the ICE, the work around is very simple (in comment above), but I figured I would report it in case it exposes something else.

Simon

 

 

Labels (1)
0 Kudos
5 Replies
Gopika_Intel
Moderator
1,361 Views

Hi,

 

Thank you for posting in Intel C++ Compiler forum. We executed the reproducer code, like mentioned it threw an error. We found out that the error is due to the initialization. Initialization inside structure is done using constructor of that structure. The structure definition declares a type and not a variable that can be initialized. The comment mentioned(std::string a {}; //This works fine.) is for declaration only. Below is the recommended way of initialization. We tried it with icpc and it was working.

 

#include <string>

#include <map>



struct StringMember {

  std::string a;

  StringMember():a(""){}



// std::string a {}; //This works fine.

};



struct C {

 StringMember mem ={};

};



struct B {

 std::map<int, C> map_ {};

};



int main () {

       return 1;

}

 

Regards

Gopika 

 

0 Kudos
sbolding
Beginner
1,351 Views
Hi Gopika,
I was using the c++11 feature "default member initializer". The automatically generated default constructor should correctly default initialize the string with "". I have worked around it, but this is a bug. These default member initializers are recommended by the cpp core guidelines now, so I expect others may stumble upon this.
0 Kudos
Gopika_Intel
Moderator
1,326 Views

Hi Simon,


We’re forwarding this thread to the internal team who can help you out.


Regards

Gopika


0 Kudos
Viet_H_Intel
Moderator
1,320 Views

I've reported this bug to our Developer.

Thank for reporting this issue.


0 Kudos
Viet_H_Intel
Moderator
981 Views


Since there is a workaround. Hence, the version against which you reported your issue is no longer targeted for fixes. Please check out the new versions of Intel oneAPI Toolkits for the latest features.

I am going to close this thread. If you have any other questions/concerns please open a new thread.

Thanks,


0 Kudos
Reply