Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of incomplete template types in templates

Tags:

c++

templates

This is follow up to a question I asked a few weeks ago in which the answer was that it is ill-formed, no diagnostic required, to use a type in a template that is only complete at the time of the template's instantiation but not at the time of its definition.

My follow up question is is this still true in the case in which the incomplete type is itself dependent on the template parameter? because it seems that it is not. The following compiles in all the compilers on Godbolt, even though foo::do_stuff() is using foo_wrapper::value() given only a forward declaration that a class template foo_wrapper will eventually exist.

#include <iostream>

template<typename T>
class foo_wrapper;

template<typename T>
class foo {
    foo_wrapper<T>& parent_;
public:
    foo(foo_wrapper<T>& wrapped) : parent_(wrapped)
    {}

    void do_stuff() {
        std::cout << "do stuff " << parent_.value() << "\n";
    }
};

template<typename T>
class foo_wrapper {
    foo<T> foo_;
    T value_;
public:

    foo_wrapper(T n) :
        foo_(*this),
        value_(n)
    {}

    void do_stuff() {
        foo_.do_stuff();
    }

    T value() const {
        return value_;
    }

};

int main()
{
    foo_wrapper<int> fw(42);
    fw.do_stuff();
}
like image 579
jwezorek Avatar asked Dec 25 '21 20:12

jwezorek


People also ask

What are the two types of templates?

There are two types of templates in C++, function templates and class templates.

What is template What are the types of templates?

There are three kinds of templates: function templates, class templates and, since C++14, variable templates. Since C++11, templates may be either variadic or non-variadic; in earlier versions of C++ they are always non-variadic.

What is the function of a templates?

Templates Specialization is defined as a mechanism that allows any programmer to use types as parameters for a class or a function. A function/class defined using the template is called a generic function/class, and the ability to use and create generic functions/classes is one of the critical features of C++.

What means empty template?

It just means the template should use the default parameter(s). For example: template <int N = 10> class X { }; X<> x; // this is an X<10>


1 Answers

This is legal.

The rule of thumb is that everything that depends on template parameters is checked when the template is instantiated. Everything else is either checked when the template is first seen, or when it's instantiated (e.g. MSVC tends to check everything late, and Clang tends to do it as early as possible).

like image 77
HolyBlackCat Avatar answered Nov 07 '22 02:11

HolyBlackCat