mardi 24 avril 2018

why does g++ handle namespaces for definitions with 'using' differently than typedefs

Using g++ 7.2.1 on Centos 7:

If I define a struct with using name = struct {...}; within a namespace I create and attempt to instantiate an object with a vector of elements of that type, I get a warning -Wsubobject-linkage. When I use a typedef, I do not get a warning. Is this correct?

(clang++ 3.4.2 does not produce this warning, for what that's worth.)

Example: why.hpp:

#include <iostream>

namespace my_ns {
    using using_using = struct {
        int     a;
        int     b;
    };
    typedef struct {
        int     a;
        int     b;
    } using_typedef;

}

class A {
public:
    std::vector<my_ns::using_using>   u = std::vector<my_ns::using_using>(8);
    std::vector<my_ns::using_typedef> t = std::vector<my_ns::using_typedef>(8);
    A() {};
};

why.cpp:

#include <vector>
#include "why.hpp"

int main() {
    A   an_a;
    std::cout << an_a.u[7].a << " " << an_a.u[7].b << std::endl;
    std::cout << an_a.t[7].a << " " << an_a.t[7].b << std::endl;
}

Compiler output:

g++ -g -O2 -Wall -pedantic -Wextra -std=c++11 -o why why
In file included from why.cpp:2:0:
why.hpp:15:11: warning: 'A' has a field 'A::u' whose type uses the anonymous namespace
    [-Wsubobject-linkage]
class A {
      ^

Aucun commentaire:

Enregistrer un commentaire