How to solve warning for variable initialization C++ - c++

I got the following warning when compiling a C++ file :
variables.cpp:10:8: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
int c{2} ;
This is the file :
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std ;
int main()
{
int a = 0 ;
int b(1) ;
int c{2} ;
string myString = "I am a string !" ;
cout << a+b+c << endl ;
cout << myString << endl ;
return EXIT_SUCCESS ;
}
And this is the command line :
g++ -std=c++0x -Wall -Wextra -Winit-self -Wold-style-cast -Woverloaded-virtual -Wuninitialized -Wmissing-declarations -Winit-self -ansi -pedantic variables.cpp -o variables
I am using g++ 7.4.0 on Ubuntu 18.04.1
I do not want to ignore the warning but to solve it,
Thank you
PS : I tried to change -std=c++0x to -std=c++11 but it did not change anything

Remove -ansi in your command, which is equivalent to -std=c++98 and would overwrite your previous flag -std=c++11. According to C-Dialect-Options,
In C mode, this is equivalent to -std=c90. In C++ mode, it is equivalent to -std=c++98.
Replace -std=c++0x with -std=c++11.
Note that if your compiler supports it, it is recommended to use the lastest c++ standard which is -std=c++17. Using newer c++ standard usually makes your code shorter, more readable and more performant.

You have 2 problem in compilation command line:
The first one is the -ansi in compilation command that implicitly set the standard to the c++98. In you case the -ansi option generate conflict with -std=c++11.
The second one is the -std=c++0x, you have to replace it with -std=c++11.

Related

Is there a compilation flag to detect duplicate lamda parameter-lambda member? [duplicate]

This question already has answers here:
Lambda capture and parameter with same name - who shadows the other? (clang vs gcc)
(2 answers)
Closed 8 months ago.
This code compiles with g++, no collision is detected, although s is the parameter captured by lambda and the lambda parameter. My compiler is g++.
gcc Version is
gcc (Debian 4.9.2-10+deb8u2) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
and I am using these compilation flags :
-W
-Wall
-ansi
-pedantic
-s
-march=native
-flto
-fwhole-program
-Wfatal-errors
-Wextra
-std=c++1y
-frename-registers
-fipa-pta
-Ofast
-pedantic-errors
-fira-loop-pressure
-fomit-frame-pointer
-fforce-addr
-falign-functions
-fno-cprop-registers
-fstrength-reduce
Code is
#include <vector>
#include <algorithm>
#include <iostream>
int main (int argc, char* argv []) {
int s (0);
std::vector<int> vec ({3, 5, 13, 1});
std::for_each (vec.begin (), vec.end (), [&s] (const int& s) {
s = s>s?s:s;
});
std::cout << "max ox v is " << s << std::endl;
}
Is there a compilation option to detect this kind of error ?
Upgrade your compilers.
Based on https://godbolt.org/z/h3Y7dW1dP that warning is missing for gcc 8.5 (with -std=c++17) and clang 7.1.0.
You should upgrade to gcc 9.1 and clang 8.0.0 or later. (Preferably a lot later.)

g++ compile options with -std=c++14 compile error showing Werror=c++11-compat

My environment Arch Linux, gcc 7.2
I'm learning C++ and I'm using keyword constexpr to define a constant, while compile, it give me an error message
error: identifier ‘constexpr’ is a keyword in C++11 [-Werror=c++11-compat]
I can compile my program with default g++, but cannot compile with -std=c++14 and -Werror
The command I'm using is:
g++ -std=c++14 -O2 -Wall -Werror -Wextra -ansi -flto
I believe the -Werror option caused the issue. but what is the issue? can someone tell me please?
#include <iostream>
int main() {
constexpr double yen_dollar = 0.107;
std::cout << yen_dollar << std::endl;
return 0;
}
test.cpp:4:5: error: identifier ‘constexpr’ is a keyword in C++11 [-Werror=c++11-compat]
constexpr double yen_dollar = 0.107;
^~~~~~~~~
test.cpp: In function ‘int main()’:
test.cpp:4:5: error: ‘constexpr’ was not declared in this scope
test.cpp:5:16: error: ‘yen_dollar’ was not declared in this scope
std::cout << yen_dollar << std::endl;
From the GCC documentation §3.4 Options Controlling C Dialect, one can read:
-ansi
In C mode, this is equivalent to -std=c90. In C++ mode, it is equivalent to -std=c++98.
Since, you compiled with
g++ -std=c++14 -O2 -Wall -Werror -Wextra -ansi -flto
-ansi overwrites -std=c++14 with -std=c++98. This is why constexpr is not recognized.
Solution: get rid of the -ansi flag.

g++ - warning: extended initializer lists only available with -std=c++11 or -std=gnu++11

I've following program.
#include <iostream>
#include <string>
int main() {
int i {0};
std::string str {"Hello World"};
std::cout << i << " : " << str << std::endl;
return 0;
}
When I compile this with g++ I got following error.
I'm using g++ 5.4. g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
I want to know what is the standard way to compile program in g++ using std::C++14 with necessary flags.
Thanks in Advance.
Update:
I've done it with following: g++ ./ex01.cpp -o ex01.out -std=c++14 -Wall -o2
Compile with the flag:
-std=c++14

Why do implicit conversions in function parameters of same size not throw a warning?

See that example:
//test.cpp
#include <iostream>
void test(unsigned int i, int j) {
std::cout << i << " " << j << std::endl;
}
int main() {
test(-1, -1);
int x = -1;
test(x,x);
return 0;
}
with:
$ g++ -Wall -Wextra -Wpedantic test.cpp:
4294967295 -1
4294967295 -1
Why does gcc let that slip? And is there an option to detect such an implicit conversion?
Cheers
This has been answered before. One of the reasons is because C allowed it, and c++ was meant to be backwards compatible. Some compilers will warn, though I tested on gcc 5.2 and it does not have an option to turn that warning on.
See: Why does C++ allows implicit conversion from int to unsigned int?
#
Just found from one of the other answers that you need to add the -Wsign-conversion flag. Seems -Wall should do that but doesn't.
Yup, I found it. Misassumed that (-Wall -Wextra -W -Wpedantic -Wconversion) would cover it all. But in
https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
the missing flag is: -Wsign-conversion

std::to_string not a member in C++11 using CygWin

I have the following code...
// My.h
#include <string>
....
// My.cpp
#include "My.h"
...
errMsg = "X value [too low]: " + std::to_string(xInVal);
But when I compile like...
//setup.py
extra_compile_args =['-std=c++11']
//console output
gcc -Wno-unused-result -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -ggdb -O2 -pipe -Wimplicit-function-declaration -fdebug-prefix-map=/usr/src/ports/python3/python3-3.4.3-1.x86_64/build=/usr/src/debug/python3-3.4.3-1 -fdebug-prefix-map=/usr/src/ports/python3/python3-3.4.3-1.x86_64/src/Python-3.4.3=/usr/src/debug/python3-3.4.3-1 -I./My -c ./My.cpp -o build/temp.cygwin-2.2.1-x86_64-3.4/./My.o -std=c++11
I get the following error....
error: ‘to_string’ is not a member of ‘std’
errMsg = "X value [too low]: " + std::to_string(xInVal);
What am I missing how do I use to_string in this way?
From the CYGWIN headers, it looks like for C++11 these are not defined unless _GLIBCXX_USE_C99 is defined They are enclosed in this if:
#if __cplusplus >= 201103L && defined(_GLIBCXX_USE_C99)
I tried defining _GLIBCXX_USE_C99 but that didn't work for my project. You can try defining it before you use the string header.
I recently had a similar issue with C++11 on CYGWIN related to __STRICT_ANSI__