Vscode-cpptools: Designated struct initializtion with default values gives an error

Created on 22 May 2020  路  11Comments  路  Source: microsoft/vscode-cpptools

Type: LanguageService

When working on an OpenGL wrapper I stumbled upon what seems to be either a bug in IntelliSense or a as of yet not implemented feature. When trying to use designated initializers for a struct, it would not work when the struct had default values. The code compiled happily, but IntelliSense gave me the error "class type not suitable for use with designators". More specifically, I get the error when one of the structs default values are omitted from the initialization. When initializing the entire struct it works fine.

  • OS and Version: Kubuntu 20.04
  • VS Code Version: 0.28.1
  • C/C++ Extension Version: 1.45.1

Silly example code

// test.cpp
struct A {
    int x = 0;
    int y = 0;
    int z = 0;
};

A func() {
    return {
        .y = 0, // error here
        .z = 0,
    };
}

Compiled with g++-10 test.cpp -c -std=c++2a
Expected behavior
There should be no such error message.

Language Service Visual Studio bug

All 11 comments

Is this .c (C) code? Or C++20?

I assume gnu++14 or equivalent, i.e. using gnu extensions. The code doesn't compile with C and IntelliSense works with C++20, but not with C++17 or older.

I assume gnu++14 or equivalent, i.e. using gnu extensions. The code doesn't compile with C and IntelliSense works with C++20, but not with C++17 or older.

Oh sorry, didn't think to actually mention what language I was using. I'm using g++ 10, with -std=c++2a. C_Cpp.default.cppStandard is c++20, so if you're saying it should work I don't know what's up.

Yeah, that should work (i.e. it's working for me). When you run C/C++: Log Diagnostics does it show c++20 as the standard version? What is the IntelliSenseMode?

Here's a dump of the Log Diagnostics:

Version: 0.28.1
Current Configuration:
{
    "name": "Linux",
    "defines": [],
    "compilerPath": "/usr/bin/clang",
    "cStandard": "c11",
    "includePath": [],
    "compilerArgs": [],
    "cppStandard": "c++20",
    "intelliSenseMode": "gcc-x64",
    "browse": {
        "path": [
            "${workspaceFolder}"
        ],
        "limitSymbolsToIncludedHeaders": true
    }
}
Translation Unit Mappings:
[ *obfuscated*/main.cpp ]:
    *obfuscated*/main.cpp
Translation Unit Configurations:
[ *obfuscated*/main.cpp ]:
    Process ID: 20081
    Memory Usage: 209 MB
    Compiler Path: /usr/bin/clang
    Includes:
        /usr/include/c++/10
        /usr/include/x86_64-linux-gnu/c++/10
        /usr/include/c++/10/backward
        /usr/local/include
        /usr/lib/llvm-10/lib/clang/10.0.0/include
        /usr/include/x86_64-linux-gnu
        /usr/include
    Standard Version: c++20
    IntelliSense Mode: clang-x64
    Other Flags:
        --clang
        --clang_version=100000
Total Memory Usage: 209 MB

There seem to be some discrepancies whether I'm actually using clang or gcc. All the settings I've managed to find have been put to gcc, so I'm at a bit of a loss. For completeness sake here is my workspace settings.json too:

{
    "files.associations": {
        "type_traits": "cpp",
        "*.ipp": "cpp",
        "*.tcc": "cpp",
        "rope": "cpp",
        "fstream": "cpp",
        "iostream": "cpp"
    },
    "C_Cpp.default.cppStandard": "c++20",
    "C_Cpp.default.intelliSenseMode": "gcc-x64",
}

Am I missing something obvious here?

Can you change your compilerPath to "/usr/bin/g++-10" or do you really want clang instead of gcc? As of clang 9 designated initializers were not implemented in C++20 so it's excluded in that case, but I can re-check if they added that for clang 10 and file a request for them to re-enable that for clang.

UPDATE: Yeah, just got added to clang 10, i.e we should fix that to match.

I wanted to use g++-10 and I thought I had changed all the settings to reflect that. Turns out I had not. Adding C_Cpp.default.compilerPath = "/usr/bin/g++" seems to have solved my issue.

Many thanks!

Also seeing this issue with Clang++10. Would love to see this supported in Clang configurations if possible. Is it a simple matter of enabling this check for Clang?

@nlieb It doesn't seem simple to me -- it's a bug with our parser You could try asking for more info at https://developercommunity.visualstudio.com/content/problem/1044574/c-intellisense-errors-occur-when-using-designated.html .

Hi! I'm having this problem too, any news?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SkyRiderMike picture SkyRiderMike  路  3Comments

vicatcu picture vicatcu  路  3Comments

chrisckc picture chrisckc  路  3Comments

jrieken picture jrieken  路  3Comments

thndrwrks picture thndrwrks  路  3Comments