mercredi 11 octobre 2017

g++ compiler optimization: cannot convert ‘

Following code not compiling:

A.h

#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>

namespace net {

        using Ip = in_addr_t;
        using Port = in_port_t;
        using SockFd = int;

        class Params final {
        public:
                Ip getIp() const { return ip_; }
                Port getPort() const { return port_; }

        private:
                Ip ip_ {INADDR_ANY};
                Port port_ {htons(5)};
        };

}

A.cpp

#include <iostream>

#include "A.h"

int main(){
        net::Params a {};

        std::cout << "Ip=" << a.getIp() << ", Port=" << a.getPort() << std::endl;

        return 0;
}

Compilation:

g++-6 -O2 -std=c++11 A.cpp

Error:

In file included from /usr/include/x86_64-linux-gnu/bits/byteswap.h:35:0,
                 from /usr/include/endian.h:60,
                 from /usr/include/ctype.h:39,
                 from /usr/include/c++/6/cctype:42,
                 from /usr/include/c++/6/bits/localefwd.h:42,
                 from /usr/include/c++/6/ios:41,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from A.cpp:1:
A.h:21:15: error: statement-expressions are not allowed outside functions nor in template-argument lists
   Port port_ {htons(5)};
               ^
In file included from A.cpp:3:0:
A.h:21:23: error: cannot convert ‘<brace-enclosed initializer list>’ to ‘net::Port {aka short unsigned int}’ in initialization
   Port port_ {htons(5)};
                       ^

But when I change port_ member variable initialization to: Port port_ {5};, g++ with -O2 compiles fine.

Above code compiles fine without optimization flag, whether port_ initialized as: Port port_ {htons(5)}; or as Port port_ {5};

Whats wrong?

Aucun commentaire:

Enregistrer un commentaire