How do I make sure I am utilizing C++14? - c++

I am trying to compile some c++ I got from a book I am going through, when I try to compile I get this warning followed by 5 related errors.
main.cpp:16:9: warning: variable templates are a C++14 extension
[- Wc++14-extensions]
int table<RecordType>::CAPACITY;
^
I have never given a thought to updating c++ or being certain of what version I am using. I am compiling this in a mac using g++.

You may enable it with -std=c++14 flag. However, your GCC version should support it in the first place. Till GCC 4.9.3, you could use -std=c++1y whereas since GCC 5.2, it supports c++14 flag as well. For more info, refer to this.

Pass the -std=c++14 flag. There are also older versions with partial C++14 support which don't support -std=c++14 yet; for these, pass the -std=c++1y flag.

You need to tell the compiler which version of the standard to compile to.
Try g++ -std=c++14.

While -std=c++14 that others are recommending will enable C++14 feature support, it will also disable a bunch of things that are enabled by default, including support for advanced POSIX APIs.
Unless you specifically want to disable G++ extensions, you should use -std=gnu++14 not -std=c++14

Related

Can't get VSCode to use a modern C++ compiler on M1 MacBook [duplicate]

I am trying to access std::popcount, but it seems like it's only there in C++ 20.
When I try compiling with g++ -std=c++20 main.cpp, it says g++: error: unrecognized command line option '-std=c++20'; did you mean '-std=c++03'
How do I tell g++ to use c++ 20?
I am using Ubuntu 18.04
C++20 features are available since GCC 8.
To enable C++20 support, add the command-line parameter
-std=c++20
For G++ 9 and earlier use
-std=c++2a
Or, to enable GNU extensions in addition to C++20 features, add
-std=gnu++20
I would try updating gcc. C++ 20 was introduced in gcc version 8 which is pretty new.
If it's an option you can update to Ubuntu 20.04 LTS which includes GCC version 9 out of the box. This would enable you to use C++ 20 and thus std::popcount
Note: use -std=c++2a in GCC 9 and earlier
there are different versions of the compiler exist and g++ is usually linked to the older one. for me, the current one is g++-9 and it clearly does not understand C++20.
C++20 requires installing gcc-10 and g++-10 (plus dependencies). assuming you already have them installed, then you need to run:
g++-10 -std=c++20 main.cpp
PS: if you want to go with v10 as default, then update links for gcc, g++ and other related ones, and use v9 (or whatever old you have) by full name.
EDIT: depending on the host OS, v11 and v12 could also be installed, but the naming is still important. replace with g++-11 or g++-12.

How to use C++ 20 in g++

I am trying to access std::popcount, but it seems like it's only there in C++ 20.
When I try compiling with g++ -std=c++20 main.cpp, it says g++: error: unrecognized command line option '-std=c++20'; did you mean '-std=c++03'
How do I tell g++ to use c++ 20?
I am using Ubuntu 18.04
C++20 features are available since GCC 8.
To enable C++20 support, add the command-line parameter
-std=c++20
For G++ 9 and earlier use
-std=c++2a
Or, to enable GNU extensions in addition to C++20 features, add
-std=gnu++20
I would try updating gcc. C++ 20 was introduced in gcc version 8 which is pretty new.
If it's an option you can update to Ubuntu 20.04 LTS which includes GCC version 9 out of the box. This would enable you to use C++ 20 and thus std::popcount
Note: use -std=c++2a in GCC 9 and earlier
there are different versions of the compiler exist and g++ is usually linked to the older one. for me, the current one is g++-9 and it clearly does not understand C++20.
C++20 requires installing gcc-10 and g++-10 (plus dependencies). assuming you already have them installed, then you need to run:
g++-10 -std=c++20 main.cpp
PS: if you want to go with v10 as default, then update links for gcc, g++ and other related ones, and use v9 (or whatever old you have) by full name.
EDIT: depending on the host OS, v11 and v12 could also be installed, but the naming is still important. replace with g++-11 or g++-12.

MinGW g++ 4.8.1-4 doesn't recognize -std=c++14

I installed MinGW by following their home page to their sourceforge and using mingw-get-setup.exe. This installed g++ 4.8.1-4. GCC 4.8 is supposed to support C++14 with a command-line switch, but I just get an "unrecognized option" error.
Is this a bug with MinGW? With GCC? What can I do about it? Since I know someone will ask, I want C++14 for its for-each loops. I'm using iterators for now, but for-each would improve both readability and writability.
EDIT: Found out my g++ build supports c++11, so I can use for-each. But still no luck on c++14 support.
g++ 4.8 simply does not support C++14, also MinGW is quite outdated when there are more new versions of gcc.
Alternatives you can use
If you want really to use C++11 or C++14 on windows with gcc you should be using one of the following options:
https://msys2.github.io/ (Uses MinGW-w64 internally).
http://mingw-w64.org/doku.php (it supports 32-bits too).
http://tdm-gcc.tdragon.net/.

C++11 functionality with MinGW

I try to use emplace() function for an unordered_map and compiler says that no such function exists.
I put -std=c+11 and it says cc1plus.exe: error: unrecognized command line option '-std=c+11'
Can i somehow use C++11 functionality with mingw?
From the GCC documentation
C++0x was the working name of a new ISO C++ standard, which was then
released in 2011 as C++11 and introduces a host of new features into
the standard C++ language and library. This project seeks to implement
new C++11 features in GCC and to make it one of the first compilers to
bring C++11 to C++ programmers.
C++11 features are available as part of the "mainline" GCC compiler in
the trunk of GCC's Subversion repository and in GCC 4.3 and later. To
enable C++0x support, add the command-line parameter -std=c++0x to
your g++ command line. Or, to enable GNU extensions in addition to
C++0x extensions, add -std=gnu++0x to your g++ command line. GCC 4.7
and later support -std=c++11 and -std=gnu++11 as well.
So, for gcc 4.3 through 4.6 use -std=c++0x, for later version use -std=c++11. Library support for map::emplace was added in gcc 4.8

Clang 3.1 and C++11 support status

From clang's C++11 support status website, http://clang.llvm.org/cxx_status.html , it says, "Initializer List" and "Lambda Expression" are all supported starting from version 3.1.
However, using LLVM/Clang trunk (3.2), compiling against initializer list and lambda expression will yield error messages.
Does anyone know if Clang >3.1 supports those features?
By default, clang++ will not enable the C++11 features - you have to pass an additional flag during compilation.
clang++ -std=c++11 [input files...]
Or
# enables some additional C++11 extensions GCC has
clang++ -std=gnu++11 [input files...]
Additionally, you can switch between using libstdc++ and Clang's own libc++, which are different implementations of the C++ standard library. libc++ in some cases might have a better implementation of the C++11 standard than your existing libstdc++ library.
# uses clang's C++ library in C++98 mode
clang++ -stdlib=libc++ [input] # uses clang's C++ library
# uses clang's C++ library and enables C++11 mode
clang++ -stdlib=libc++ -std=c++11 [input]
The latter is important if you're using Clang in an environment with an outdated version of libstdc++ (like Mac OSX), but note that the two C++ libraries are not compatible with each other, so you would have to rebuild any dependencies against libc++ if you were to use that.
The page at http://clang.llvm.org/cxx_status.html is confusing at best. Currently, the released 3.1 version does not support initializer lists or lambdas (so I've switched back to GCC 4.8 for the time being).
You can always check clang support for features using the __has__feature macro, according to the instructions here:
http://clang.llvm.org/docs/LanguageExtensions.html#checking_language_features
For example, __has_feature(cxx_generalized_initializers) or __has_feature(cxx_lambdas) will return true if those features are available and enabled.
Personally, I'm expecting those features to be ready by clang 4.0, which is expected to be released with the next Xcode (likely June 2012).
-- Edited to clarify the versions I've been testing -- clearly, clang versioning is more complex than I had realized.