glades
glades

Reputation: 4769

Forward declarations don't work the same as in-place forward declarations when used in class context

I'm poking around in the myst of C++ instantiation / declaration order. Here's a fun bit I came across:

This compiles :

#include <cstddef>
#include <variant>
#include <array>

template <size_t V>
struct container
{
    // THIS COMPILES
    struct array;
    using val = std::variant<std::monostate, int, array>;

    // THIS DOESNT
    // using val = std::variant<std::monostate, int, struct array>;

    struct proxy : val
    {
        using val::variant;
    };

    struct array { };
};

int main()
{
    container<10> ctr;
}

But when you opt for in-place declarations, it suddenly stops working (Demo):

#include <cstddef>
#include <variant>
#include <array>

template <size_t V>
struct container
{
    // THIS COMPILES
    // struct array;
    // using val = std::variant<std::monostate, int, array>;

    // THIS DOESNT
    using val = std::variant<std::monostate, int, struct array>;

    struct proxy : val
    {
        using val::variant;
    };

    struct array { };
};

int main()
{

    container<10> ctr;
}

This is the error I get:

/opt/compiler-explorer/gcc-trunk-20220729/include/c++/13.0.0/type_traits:1012:52: error: static assertion failed: template argument must be a complete class or an unbounded array
 1012 |       static_assert(std::__is_complete_or_unbounded(__type_identity<_Tp>{}),
      | 

Can someone explain me exactly why this happens? What is the difference?

EDIT: You are allowed in certain circumstances to declare a type in a template argument list:

#include <cstddef>
#include <variant>
#include <array>
#include <cstdio>

void foo(std::initializer_list<struct array>);

struct array
{
    array(int a) : a_{a} {}
    void print() {
        printf("%d\n", a_);
    }
    int a_;
};

void foo(std::initializer_list<struct array> init) {
    for (auto a : init) {
        a.print();
    }
    printf(".. it works\n");
}

int main()
{
    foo({1,2,3});
}

I don't know when and where this applies though.

Upvotes: 1

Views: 214

Answers (1)

Brian Bi
Brian Bi

Reputation: 119174

A declaration of the form

class-key attribute-specifier-seq(opt) identifier;

(where class-key means struct, class, or union), declares the identifier in the scope where the declaration appears ([dcl.type.elab]/2). That means in your first code snippet, array is forward declared as a member class of the container class template and you can later define it within the same scope.

When an elaborated-type-specifier such as struct array appears as a component of some larger declaration or expression, [dcl.type.elab]/3 applies. First, the compiler looks up the name array, ignoring anything that is not a type ([basic.lookup.elab]/1). If it doesn't find anything, then it forward-declares array in "the nearest enclosing namespace or block scope". Consequently, the struct array that you later define as a nested class is a different class, and the one you referred to as struct array earlier has no definition.

Upvotes: 3

Related Questions