skip to Main Content

I have a question about why I’m getting the two problems, this declaration has no storage class or type specifier, and expected a ‘;’ ln 3 col 11, of the code: Main.cpp:

#include <iostream>

consteval int get_value(){
    return 3;
}

int main(){
    constexpr int value = get_value();
    std::cout << "value : " << value << std::endl;
    return 0;
}

For some reason with this code my compiler doesn’t detect consteval as a valid int.

Tasks.Json file


{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "Build with MSVC",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/std:c++20",
                "/EHsc",
                "/Fe:",
                "${fileDirname}\rooster.exe",
                "${workspaceFolder}\*.cpp"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": "build",
            "detail": "compiler: cl.exe"
        }
    ]
}

c_cppp_properties.json


{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe",
            "cStandard": "c17",
            "cppStandard": "c++20",
            "intelliSenseMode": "windows-msvc-x64"
        }
    ],
    "version": 4
}

I’m using cl.exe to compile and run task on visual studio code. my compiler cl.exe version is Microsoft (R) C/C++ Optimizing Compiler Version 19.41.34120 for x86. Any solutions are greatly appreciated, thanks!

I’ve tried changing cpp standard from c++latest to c++20, because I thought that consteval wasn’t being detected because c++20 wasn’t a confirmed standard.

 *  Executing task: Build with MSVC 

Starting build...
cmd /c chcp 65001>nul && cl.exe /Zi /std:c++20 /EHsc /Fe: C:Scriptsc++ScriptsThe-C-20-Masterclass-Source-Code-mainThe-C-20-Masterclass-Source-Code-main2.EnvironmentSetup1.Windows4.V_s_CodeMsvcConfigurationrooster.exe C:Scriptsc++ScriptsThe-C-20-Masterclass-Source-Code-mainThe-C-20-Masterclass-Source-Code-main*.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.41.34120 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

*.cpp
c1xx: fatal error C1083: Cannot open source file: 'C:Scriptsc++ScriptsThe-C-20-Masterclass-Source-Code-mainThe-C-20-Masterclass-Source-Code-main*.cpp': No such file or directory

Build finished with error(s).

 *  The terminal process failed to launch (exit code: -1). 
 *  Terminal will be reused by tasks, press any key to close it

2

Answers


  1. Chosen as BEST ANSWER

    As one commenter mentioned:

    The first link showed 3 compilers. gcc, clang and msvc v19 latest (which means the compiler in Visual Studio 2022). You are using a msvc version from Visual Studio 2019: "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe"

    The answer to the question was to change the compiler path to 2022, as suggested here, thanks! C:Program FilesMicrosoft Visual Change the compiler path to Studio2022CommunityVCToolsMSVC14.41.34120binHostx64x64


  2. The issue you’re encountering is likely related to the compiler version you’re using. The consteval keyword was introduced in C++20, but not all compilers fully support every feature of the standard, even if they claim C++20 support

    Compiler Version Compatibility: Make sure that the version of the MSVC compiler you’re using fully supports consteval. Even though you’re using the C++20 standard, some older versions of MSVC might not fully implement all C++20 features. Consider updating your Visual Studio installation to the latest version.

    You mentioned you’re using /std:c++20 in your tasks.json. This is correct, but if your compiler doesn’t fully support consteval, you might encounter issues. Try switching to the latest preview standard with /std:c++latest to see if it makes any difference.

    Update or Reinstall the Compiler if updating the Visual Studio version doesn’t help, you might try reinstalling the MSVC compiler or switching to a different compiler, like GCC or Clang, to see if the issue persists.Try updating your MSVC toolchain to the latest version and see if the problem persists. If it does, switching to /std:c++latest or trying a different compiler might help identify the root cause.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search