thumala manish
thumala manish

Reputation: 318

Why std::size() is not a member of std in gcc 8.2.0

I'm trying to teach myself some C++17.

Why is the compiler throwing an error for the below code snippet?

#include <iostream> 
#include <vector>
#include <iterator>

int main() 
{
    std::vector<int> v = { 3, 1, 4 };
    std::cout << std::size(v) << '\n'; 

    int a[] = { -5, 10, 15 };
    std::cout << std::size(a) << '\n';
}

The compiler throws the following error

manish@Manish-Tummala:~/c_files$ g++ 6.cpp -o - 6.out
6.cpp: In function ‘int main()’:
6.cpp:8:23: error: ‘size’ is not a member of ‘std’
     std::cout << std::size(v) << '\n';
                       ^~~~
6.cpp:8:23: note: suggested alternative: ‘size_t’
     std::cout << std::size(v) << '\n';
                       ^~~~
                       size_t
6.cpp:11:23: error: ‘size’ is not a member of ‘std’
     std::cout << std::size(a) << '\n';
                       ^~~~
6.cpp:11:23: note: suggested alternative: ‘size_t’
     std::cout << std::size(a) << '\n';
                       ^~~~
                       size_t

Upvotes: 12

Views: 8215

Answers (3)

Florian Weimer
Florian Weimer

Reputation: 33719

For C++17 support in GCC, please refer to:

The C++17 compilation mode is the default starting with GCC 11.1. In earlier GCC versions, it is possible to enable with a command-line parameter:

To enable C++17 support, add the command-line parameter -std=c++17 to your g++ command line. Or, to enable GNU extensions in addition to C++17 features, add -std=gnu++17.

Note that for GCC versions before GCC 9.1, the C++ library ABI was still considered unstable, so if you build and link your application with an earlier compiler, it may not work correctly with a different libstdc++ run-time library version (from a different GCC release, such as introduced by an operating system upgrade).

Upvotes: 12

Aditya
Aditya

Reputation: 191

For VSCode: Add an explicit reference to C++17 would help which can be done by modifying the tasks.json file in the folder

  "args": [
           "-std=c++17",
           "${file}",
            "-o",
            "${fileDirname}\\${fileBasenameNoExtension}.exe"
        ]

Upvotes: 1

nobody
nobody

Reputation: 1287

Your g++ installation needs to be at version 6 or higher. You can check it with

g++ -v

If your g++ version is high enough. You must also execute it with the c++17 command line option.

g++ -std=c++17 6.cpp -o 6.out

or

g++ -std=gnu++17 6.cpp -o 6.out

Upvotes: 1

Related Questions