ipr icon indicating copy to clipboard operation
ipr copied to clipboard

Template declarations with no definition.

Open d-winsor opened this issue 4 years ago • 3 comments

Similar to issue #21 a forward declaration of a function template (with no body) is not possible as mapping is assumed to always have a result. It is similarly unclear how to represent class template decelerations with no definition.

template<typename T = int> void foo(T x);
template<typename T> void foo(T y) { };

template<typename T = int>  struct classTemplate;
template<typename T> struct classTemplate{ };

In the case of function template mapping the .result() should be a mapping of Parameters to a non-existent body. In the case of a class template mapping the .result() should be nothing

A possible solution to this is to make the result optional which was also suggested in #21.

d-winsor avatar Nov 24 '20 21:11 d-winsor

A template declaration is a mapping that always produces a result: the declaration being parameterized (e.g. the current instantiation in the case of a class template definition). That declaration being parameterized may not have a definition itself, but the template is always a mapping.

So given

the template foo is a named mapping that takes a type (defaulted to int) and produces a function declaration.

Similarly,

template<typename T> void foo(T y) { }

is a named mapping that takes a type and produces the function definition.

In both cases the mapping is the mapping taking a type and producing a declaration.

Same logic for the class template declaration vs. class template definition.

GabrielDosReis avatar Nov 25 '20 22:11 GabrielDosReis

I agree with the explanation of templates conceptionally and I misused definition in this case. The question I had is focused on how to represent a forward declaration of a template mapping and how that interacts with the current interface without resulting in a crash.

Let's focus on the code snippet below in isolation. To my understanding this would be represented as a Template Decl node which is a mapping of the template param T to the mapping of the function (param 'x' -> void). The problem here is that mapping of the function (not the template) has no result itself despite a Mapping being defined as always having a result in the interface.

template<typename T = int> void foo(T x);

So nothing about the Template Decl node is invalid but if you were to call util::view<Mapping>(template.result())->result() on the above will result in a crash.

d-winsor avatar Dec 02 '20 20:12 d-winsor

Since the definition here is missing - because it is just a non-defining declaration - isn't a Nothing node an appropriate value for the result()

GabrielDosReis avatar Feb 16 '21 20:02 GabrielDosReis