extended initializer lists being seen as functions - c++

I'm trying to compile an opensource project but I'm getting this problem from g++
error: function definition does not declare parameters
the code is something like this
#include <iostream>
namespace hi {
class hello {
public:
bool first { true };
};
}
int main(int argc, char *argv[])
{
hi::hello h
std::cout << "output: " << h.first << std::endl;
return 0;
}
which produces the same compilation problem as the code of the opensource project when compiled with
g++ -O2 bools.cpp -o bools -std=c++0x
however if I try to compile this code with the same options it compiles and runs as it should
#include <iostream>
int main(int argc, char *argv[])
{
bool value { true };
std::cout << "output: " << value << std::endl;
return 0;
}
I'm using g++ 4.6.3 on Ubuntu 64bit.
thanks for your time.

Support for non-static data member initializers was added in gcc 4.7. You can see the list of what C++0x/C++11 features gcc supports with what versions here. The line for this feature says:
Non-static data member initializers | N2756 | GCC 4.7
This works perfectly fine in gcc 4.7 and greater, see it live here.

Related

C string as template non-type parameter works in gcc 6.3 but does not work in Visual Studio 2017 (19.16.27027.1 for x64)

The following code:
#include <iostream>
template<const char* Pattern> void f() {
std::cout << Pattern << "\n";
}
static constexpr const char hello[] = "Hello";
int main() {
f<hello>(); //Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27027.1 for x64
// Copyright (C) Microsoft Corporation. All rights reserved.
//
// string-as-template-parameter.cpp
// string-as-template-parameter.cpp(10): fatal error C1001: An internal error has occurred in the compiler.
// (compiler file 'msc1.cpp', line 1518)
// To work around this problem, try simplifying or changing the program near the locations listed above.
// Please choose the Technical Support command on the Visual C++
return 0;
}
works when compiled by gcc (g++ (Debian 6.3.0-18+deb9u1) 6.3.0 20170516) but results in C1001 when compiled by VS 2017.
As a workaround I use:
#include <iostream>
template<const char** Pattern> void f() {
std::cout << *Pattern << "\n";
}
static const char* hello = "Hello";
int main() {
f<&hello>();
return 0;
}
Does anybody have idea of more beautiful solution? May be initial code has an error which is skipped by gcc?
Does anybody have idea of more beautiful solution?
You can use a reference to std::string instead.
#include <iostream>
#include <string>
template<std::string & Pattern> void f() {
std::cout << Pattern << "\n";
}
static std::string hello = "Hello";
int main() {
f<hello>();
return 0;
}
This compiles with MSVC in Visual Studio.
This works because as per Cppreference, a named lvalue reference with linkage is allowed as a non-type parameter. (Note that hello is not local.)

Possible clang compiler bug with thread_local in templates

In my project I need separate thread-local storage for each instance of a data member. Because I ran into problems while implementing this functionality, I extracted a simplified version of the code into the following C++14 program:
#include <iostream>
#include <unordered_map>
#include <vector>
template<class T> class ThreadLocalMember
{
public:
T& local() { return store.map[this]; }
private:
struct Store
{
Store() { std::cout << "construct" << std::endl; }
~Store() { std::cout << "destruct" << std::endl; }
std::unordered_map<ThreadLocalMember<T>*, T> map;
};
static thread_local Store store;
};
template <class T> thread_local typename ThreadLocalMember<T>::Store ThreadLocalMember<T>::store;
int main()
{
ThreadLocalMember<int> counter;
std::cout << "point 1" << std::endl;
int result = counter.local();
std::cout << "point 2; result: " << result << std::endl;
return result;
}
The expected output is
point 1
construct
point 2; result: 0
destruct
However, when compiled with clang Apple LLVM version 9.1.0 (clang-902.0.39.1) on MacOS High Sierra 10.13.4 using
clang++ -std=c++14 -O3 ThreadLocalMember.cpp -o test
(or with -O1 or -O2) the output is:
point 1
Illegal instruction: 4
It seems that the constructor of the thread_local variable is never executed and the program crashes when the variable is first accessed.
The problem goes away when
the program is compiled without optimisation (which is not acceptable in production mode)
the template class is replaced by a regular class (a possible workaround but very annoying)
the thread_local keyword is removed in both places (but then the program no longer does what I need it to do when there are multiple threads)
The program also compiles and runs fine when using gcc 5.4.0 on Ubuntu 16, with or without optimisation flag.
Is there something wrong with my code, or am I looking at a clang compiler bug?

C++ on ubuntu hello world

I'm trying to write my first code on ubuntu terminal using c++
.I created a new cpp file named aaa by
"nano aaa.cpp"
then inside I wrote
#include<iostream>
using std::cout;
using std::endl;
int main(int argc, car** argv)
{
cout << "hello" << endl;
return 0;
}
i saved and got out but when i tried typing
g++ aaa.cpp
I got the error
error: ‘endl’ was not declared in this scope
cout << "hello" << endl;
where did I go wrong
I tried
$ sudo apt-get remove g++ libstdc++-6.4.7-dev
$ sudo apt-get install build-essential g++-multilib
but it was no good
any help?
Stylistically, I prefer to be explicit: std::cout and std::endl.
#include <iostream>
int main(int argc, char** argv) {
std::cout << "hello" << std::endl;
return 0;
}
This also fixes a tyo of yours: char, not car and repairs the #include.
This works as expected:
$ g++ -Wall -pedantic -o foo2 foo2.cpp
$ ./foo2
hello
$
If you wanted to, you could also use
using namespace std;
but as stated, I prefer to more explicit form.
Edit: Nothing as much fun as debating the beancounters. OP question is likely having _another error he is not sharing. His code, repaired for char actually builds:
$ cat foo3.cpp
#include <iostream>
using std::cout;
using std::endl;
int main(int argc, char** argv) {
cout << "hello" << endl;
return 0;
}
$ g++ -Wall -pedantic -o foo3 foo3.cpp
$ ./foo3
hello
$
Ubuntu 16.04, g++ 5.4.0
First, make sure you have the tools you need to be able to compile a C++ code on Ubuntu. For that run the following code in the command line :
This line will install all the basic stuff you need for compiling a C++ code, it will install C, C++, and make.
sudo apt-get install build-essential
Now that you have all you need, I will suggere to explicetely using std::cout / std::endl . That way you don't import all the stuff available under the namespace std that you are not using. Using std::cout / std::endl shows clearly the origin the instance you are using.
Notice : you have an error in the main function argument, namely : car, it should be char
#include<iostream>
int main(int argc, char** argv)
{
std::cout << "hello" << std::endl;
return 0;
}
Now you can compile and run your code this way :
in this example I'm calling the executable file "hello"
g++ -Wall -o hello aaa.cpp
./hello

lowest() is not a member of std::numeric_limits

I am trying to compile the following code:
#include <iostream>
#include <limits>
int main()
{
std::cout << std::numeric_limits<int>::lowest() << std::endl;
}
and I get the following error:
../main.cpp:5: error: 'lowest' is not a member of 'std::numeric_limits<int>'
cout << std::numeric_limits<int>::lowest() << std::endl;
^
I am using QT Creator 3.1.1 on Ubuntu 15.04, the compiler is set to GCC by default (/usr/bin/g++).
Anyone have an idea what could be the problem?
The lowest function was introduced in the C++11 standard, so you need to enable C++11 compatibility with the -std=c++11 flag (it's not enabled by default).

BOOST_THROW_EXCEPTION causing Abort Trap

I'm trying to use boost::exception and have condensed my prototype code down to the example in the Boost exception tutorial however when running the code with the BOOST_THROW_EXCEPTION macro I get an abort from the program.
#include <iostream>
#include <boost/exception/all.hpp>
typedef boost::error_info<struct tag_my_info,int> my_info;
struct my_error: virtual boost::exception, virtual std::exception { };
void f() {
BOOST_THROW_EXCEPTION(my_error() << my_info(42));
// Uncomment the below (and comment the above) for the program to work
//throw my_error() << my_info(42);
}
int main(int argc, char** argv) {
try {
f();
}
catch(my_error& x) {
if(int const* mi = boost::get_error_info<my_info>(x)) {
std::cout << "My info: " << *mi << std::endl;
}
}
return 0;
}
Running the code with the BOOST_THROW_EXCEPTION macro:
$ ./a.out
Abort trap
If as the comment says, I swap the code, all is well
$ ./a.out
My info: 42
Below is the output from the g++ preprocessor for f()
void f() {
::boost::exception_detail::throw_exception_(my_error() << my_info(42),__PRETTY_FUNCTION__,"main.cpp",14);
}
Software versions are:
$ g++ -v
Using built-in specs.
Target: x86_64-apple-darwin10
Thread model: posix
gcc version 4.4.6 (GCC)
$ port list boost
boost #1.47.0 devel/boost
I'm on OSX SL using the tools provided by MacPorts. I've double checked the g++ search paths and there's only one copy of the boost hpp files and that's the ones that belong to the aforementioned boost package.
I have no idea why the abort trap is being called. I admit I'm newish to C++ .
The problem was caused by using the MacPorts version of g++. There are plenty of tickets related to exceptions and Abort Traps in the MP system (and plenty of examples on Google).
Using the version of g++ that comes with XCode enabled this problem to go away.