vendredi 19 août 2016

Define partial specialization for some external classes with restriction for template parameters

I have a class Foo and class Bar. They are wrappers over std::array. Both of them have some derived classes.

template<typename T, std::size_t N>
struct Foo {
    std::array<T, N> data;
};

template<typename T, std::size_t N>
struct FooDerived : Foo <T, N> {};

template<typename T, std::size_t N>
struct Bar {
    std::array<T, N> data;
};

template<typename T, std::size_t N>
struct BarDerived : Bar <T, N> {};

And I want to implement tuple-interface in std namespace: get / tuple_size / tuple_element. But these methods should be available only for Foo and derived from Foo classes.

template<template<typename, std::size_t> class T, typename TArg, std::size_t NArg>
class tuple_size<T<TArg, NArg>>
  : public integral_constant<std::size_t, NArg>
  {
  };

template<std::size_t I, template<typename, std::size_t> class T, typename TArg, std::size_t NArg>
struct tuple_element<I, T<TArg, NArg>>
  {
  using type = TArg;
  };

That works, but works also with Bar and Bar-derived classes.

I thought to use std::enable_if with std::is_base_of. std::enable_if for classes can be used as a template parameters. And if I write:

template<template<typename, std::size_t> class T, typename TArg, std::size_t NArg,
    typename std::enable_if<std::is_base_of<A<TArg, NArg>, T<TArg, NArg>>>::type = 0>
class tuple_size<T<TArg, NArg>>
  : public integral_constant<std::size_t, NArg>
  {
  };

it leads to compile error: default template arguments may not be used in partial specializations.

Is it possible to forbid to use tuple-like interface for non-related to Foo classes?

Example: http://ift.tt/2btBN2S

Aucun commentaire:

Enregistrer un commentaire