mardi 17 septembre 2019

Compiling C++11 code on older versions of gcc

We are trying to build a C++ project which was recently modified to mark some destructor as noexcept on a legacy system which uses gcc 4.5.4.

The use of this compiler version is mandatory, so upgrading is not a solution.

The only C++11 feature that is in the code is the noexcept specifier for destructors.

I have read that while 4.5 does not support the standard -std=c++11, it does support the experimental (at the time) flag -std=gnu++0x . However, using this to compile a simple test project that uses the noexcept specifier does not allow the code to compile either. The code for this example is available below.

test.h:

class Test
{
public:
    Test();
    ~Test() noexcept;
};

test.cpp:

#include "test.h"
#include <iostream>

using namespace std;

Test::Test()
{
  cout<<"Ctor"<<endl;
}

Test::~Test() noexcept
{
  cout<<"Dtor"<<endl;
}

main.cpp:

#include "test.h"

int main(int argc, char **argv)
{
 {
  Test t;
 }

 return 0;
}

I guess the experimental flag i used should have been the main solution here, but i wanted to check here if maybe i am missing any alternatives.

If it's just not possible to compile this code using gcc 4.5.4, my solution would be to declare a macro that applies noexcept only under a certain condition which would evaluate to true only on the legacy build system. But i am open to alternatives.

The error i am getting when using this compiler, btw, is:

# g++ -std=c++0x test.cpp main.cpp -o runme
In file included from test.cpp:1:0:
test.h:5:17: error: expected ‘;’ before ‘noexcept’
test.cpp:11:15: error: expected initializer before ‘noexcept’
In file included from main.cpp:1:0:
test.h:5:17: error: expected ‘;’ before ‘noexcept’

Aucun commentaire:

Enregistrer un commentaire