c++20 and c++11 standards both specified in tasks.json, still won't work?

Viewed 58

I'm learning C++ using Visual Studio Code. I'm starting to pull my hair out trying to figure why the compiler won't recognize C++11 standards. The code + .json file + error are as follows:

#include <iostream>
#include <vector>
using namespace std;

int main() {

    vector <int> vector_numbers;

    for (int i : vector_numbers) {
        vector_numbers[1] = i + 1;
    }

    for (int i : vector_numbers) {
        cout << "The vector element at index [" << i << "] is " << vector_numbers.at(i) << endl;
    }

    return 0;
}

The contents of "args" parameter in tasks.json is

            "args": [
                "-std=c++11",
                "-std=c++17",
                "-std=c++20",
                "-stdlib=libc++",
                "-fcolor-diagnostics",
                "-fansi-escape-codes",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ]

and error is:

<REDACTED> % cd "<REDACTED PATH>" && g++ Vectors.cpp -o Vectors && "<REDACTED PATH>"Vectors
Vectors.cpp:9:16: warning: range-based for loop is a C++11 extension [-Wc++11-extensions]
    for (int i : vector_numbers) {
               ^
Vectors.cpp:13:16: warning: range-based for loop is a C++11 extension [-Wc++11-extensions]
    for (int i : vector_numbers) {
               ^
2 warnings generated.
1 Answers

OK, issue resolved and posting solution for posterity.

The issue was with coderunner extension not using -std=c++20 in the CLI argument. For coderunner's settings.json file should be:

"code-runner.executorMap": {

        "javascript": "node",
        "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
        "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "cpp": "cd $dir && g++ **-std=c++20** $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "php": "php",
        "python": "python -u",
        "perl": "perl",
        "perl6": "perl6",
        "ruby": "ruby",
        "go": "go run",
        "lua": "lua",
        "groovy": "groovy",
        "powershell": "powershell -ExecutionPolicy ByPass -File",
        "bat": "cmd /c",
        "shellscript": "bash",
        "fsharp": "fsi",
        "csharp": "scriptcs",
        "vbscript": "cscript //Nologo",
        "typescript": "ts-node",
        "coffeescript": "coffee",
        "scala": "scala",
        "swift": "swift",
        "julia": "julia",
        "crystal": "crystal",
        "ocaml": "ocaml",
        "r": "Rscript",
        "applescript": "osascript",
        "clojure": "lein exec",
        "haxe": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
        "rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",
        "racket": "racket",
        "scheme": "csi -script",
        "ahk": "autohotkey",
        "autoit": "autoit3",
        "dart": "dart",
        "pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
        "d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
        "haskell": "runhaskell",
        "nim": "nim compile --verbosity:0 --hints:off --run",
        "lisp": "sbcl --script",
        "kit": "kitc --run",
        "v": "v run",
        "sass": "sass --style expanded",
        "scss": "scss --style expanded",
        "less": "cd $dir && lessc $fileName $fileNameWithoutExt.css",
        "FortranFreeForm": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran-modern": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran_fixed-form": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "sml": "cd $dir && sml $fileName"
    }

Key config param is -std=c++20 for "cpp".

Related