'Use of incomplete types in templates

Is it legal to use an incomplete type in a template if the type is complete when the template is instantiated?

As below

#include <iostream>

struct bar;

template <typename T>
struct foo {

    foo(bar* b) : b(b) {
    }
    
     void frobnicate() {
          b->frobnicate();
     }

     T val_;
     bar* b;
};

struct bar {
     void frobnicate() {
          std::cout << "foo\n";
     }
};

int main() {
    bar b;
    foo<int> f(&b);
    f.frobnicate();
    return 0;
}

Visual Studio compiles the above without complaining. GCC issues the warning invalid use of incomplete type 'struct bar' but compiles. Clang errors out with member access into incomplete type 'bar'.



Solution 1:[1]

Clang is correct in reporting an error (as opposed to a warning or being silent about it), though MSVC's and GCC's behavior are also consistent with the standard. See @HolyBlackCat's answer for details on that.

The code you posted is ill-formed NDR. However, what you want to do is feasible.

You can defer the definition of template member functions the same way you would for a non-template class. Much like non-template classes, as long as these definitions requiring bar to be a complete type happen only once bar is complete, everything is fine.

The only hiccup is that you need to explicitly mark the method as inline to avoid ODR violations in multi-TU programs, since the definition will almost certainly be in a header.

#include <iostream>

struct bar;

template <typename T>
struct foo {

    foo(bar* b) : b(b) {
    }
    
    inline void frobnicate();

    T val_;
    bar* b;
};

struct bar {
     void frobnicate() {
          std::cout << "foo\n";
     }
};

template <typename T>
void foo<T>::frobnicate() {
     b->frobnicate();
}

int main() {
    bar b;
    foo<int> f(&b);
    f.frobnicate();
    return 0;
}

Solution 2:[2]

The code is ill-formed, no diagnostic required.

[temp.res.general]/6.4

The validity of a template may be checked prior to any instantiation.

The program is ill-formed, no diagnostic required, if:
...
— a hypothetical instantiation of a template immediately following its definition would be ill-formed due to a construct that does not depend on a template parameter, ...


If you absolutely can't define bar before the template, there is a workaround: you can introduce an artifical dependency on the template parameter.

template <typename T, typename, typename...>
struct dependent_type {using type = T;};
template <typename T, typename P0, typename ...P>
using dependent_type_t = typename dependent_type<T, P0, P...>::type;

Then use dependent_type_t<bar, T> instead of bar.

Solution 3:[3]

If you want to customise a template using a forward declaration as a temlpate argument, you can do this (wihtout warnings or errors):

template <typename T, typename = T>
class print_name;

so when you do a partial specialization, you use the second, unspecialized template parameter for your calls:

struct john;

template <typename T>
class print_name<john, T>
{
public:
    void operator()(const T& f) const
    {
        std::cout << f.name << std::endl;
    }
};

In this context T is not incomplete. But when you instantiate print_name<john>, SFINAE will kick.

Here is a full example:


#include <iostream>

template <typename T, typename = T>
class print_name;

struct john;

template <typename T>
class print_name<john, T>
{
public:
    void operator()(const T& f) const
    {
        std::cout << f.name << std::endl;
    }
};

struct slim;

template <typename T>
class print_name<slim, T>
{
public:
    void operator()(const T& f) const
    {
        std::cout << f.myName << std::endl;
    }
};


#include <string>

struct john
{
    std::string name;
};

struct slim
{
    std::string myName;
};

int main()
{
    print_name<john>{}(john{"John Cena"});
    print_name<slim>{}(slim{"Slim Shady"});
    return 0;
}

https://godbolt.org/z/czcGo5aaG

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 HolyBlackCat
Solution 3 Sergey Kolesnik