Boost logo

Boost :

Subject: Re: Improving diagnostics on implicit instantiation of undefined template
From: Mateusz Loskot (mateusz_at_[hidden])
Date: 2019-02-21 21:44:32


On Thu, 21 Feb 2019 at 22:01, Mateusz Loskot <mateusz_at_[hidden]> wrote:
>
> One obvious tool for such purpose is `static_assert`.
> Simplifying the whole situation quite a lot, here it is
>
> // somewhere in detail/utility.hpp
> namespace detail {
> template <typename T>
> struct undefined_specialization : std::false_type {};
> } // namespace detail
>
> // in dynamic_step.hpp
> template <typename IteratorOrLocatorOrView>
> struct dynamic_x_step_type
> {
> static_assert(
> detail::undefined_specialization<IteratorOrLocatorOrView>::value,
> "dynamic_x_step_type is not specialised for given type");
> };

There is one improvement possible, which Joel Falcou suggested
on #meta_programming_at_[hidden]:

Add the following to the definition of the 'undefined' base template

template<typename> struct error_with;
using type = error_with<T>;

See this in action: https://godbolt.org/z/DIBREx

This removes potentially very long error about undefined/incomplete type,
compare

```
<source>: In instantiation of 'struct trait1<not_feature<int> >':
<source>:29:8: required from 'struct trait2<not_feature<int> >'
<source>:34:40: required from here
<source>:16:9: error: static assertion failed: no trait defined for given T
         undefined_specialization<T>::value,
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>: In function 'int main()':
<source>:34:42: error: 'type' in 'struct trait2<not_feature<int> >'
does not name a type
     using t1 = trait2<not_feature<int>>::type;
                                          ^~~~
```

vs

```
<source>: In instantiation of 'struct trait1<not_feature<int> >':
<source>:30:8: required from 'struct trait2<not_feature<int> >'
<source>:35:40: required from here
<source>:16:9: error: static assertion failed: no trait defined for given T
         undefined_specialization<T>::value,
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
```

To summary, I like Joel's refinement of my earlier suggestion.

Best regards,

-- 
Mateusz Loskot, http://mateusz.loskot.net

Boost list run by Boost-Gil-Owners