C++ Logo

std-proposals

Advanced search

Re: Explicit alias template specialization

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Fri, 19 Nov 2021 15:46:18 +0100
pt., 19 lis 2021 o 14:48 Nicolas Weidmann via Std-Proposals
<std-proposals_at_[hidden]> napisał(a):
>
> Hello,
>
> Currently most templates can be fully specialized, however, alias templates cannot.
>
>
>
> As the following examples illustrate, the problem alias templates specialization would solve can currently be mimicked using alias traits.
>
>
>
> But adding alias templates specialization would render to code more straightforward and readable:
>
>
>
> The first example uses a default alias template followed by specializations.
>
> The second example ‘deletes’ the default alias template, only allowing for specialized versions.
>
>
>
> The first example achieved using traits:
>
>
>
> ///////////////////////////////////////////////////////////////
>
>
>
> template<typename T> struct AliasTrait {
>
> using type = std::set<T>;
>
> };
>
>
>
> template<> struct AliasTrait<int> {
>
> using type = std::vector<int>;
>
> };
>
>
>
> template<> struct AliasTrait<double> {
>
> using type = std::list<double>;
>
> };
>
>
>
> template<typename T> void foo()
>
> {
>
> typename AliasTrait<T>::type values;
>
> }
>
>
>
> The first example achieved using alias template specialization:
>
>
>
> ///////////////////////////////////////////////////////////////
>
>
>
> template<typename> using alias = std::set<T>;
>
>
>
> template<> using alias<int> = std::vector<int>;
>
> template<> using alias<double> = std::list<double>;
>
>
>
> template<typename T> void foo()
>
> {
>
> alias<T> values;
>
> }
>

Why not simply:
```
template<typename> using alias = typename AliasTrait<T>::type;
```
And this was one of the reasons why we have aliases.
What will we grain exactly if we make aliases more complex?

>
>
> The second example achieved using traits:
>
>
>
> ///////////////////////////////////////////////////////////////
>
>
>
> template<typename> structNoDefaultAliasTrait;
>
>
>
> template<> struct NoDefaultAliasTrait<int> {
>
> using type = std::vector<int>;
>
> };
>
>
>
> template<> structNoDefaultAliasTrait<double> {
>
> using type = std::list<double>;
>
> };
>
>
>
> template<typename T> void bar()
>
> {
>
> typename NoDefaultAliasTrait<T>::typevalues;
>
> }
>
>
>
> The second example achieved using alias template specialization and introducing ‘deletion’ of the generic alias template:
>
>
>
> ///////////////////////////////////////////////////////////////
>
>
>
> template<typename> using no_default_alias= delete;
>
>
>
> template<> using no_default_alias<int> = std::vector<int>;
>
> template<> using no_default_alias<double> = std::list<double>;
>
>
>
> template<typename T> void bar()
>
> {
>
> no_default_alias<T> values;
>
> }
>
>
>
> Regards,
>
>
>
> Nicolas
>
>
> 
> 
>
> Sent from my iPhone
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2021-11-19 08:46:31