1

次の形式でテンプレート パラメータを指定したときにaccepted_dense_vector<??>::value戻りたい:'true'

C<T>いつCでもuvector、ということdynamic_arrayで。Tstd::is_arithmetic

std::array<T,S>いつTですかstd::is_arithmetic

container_reference<C>いつaccepted_dense_vector<C>::valueが「真」です。

parentすべて正常に動作しますが、完全にインスタンス化された派生クラス A、C、D では、回避策の定義を削除したいと考えています。

これどうやってするの?

#include <iostream>
#include <array>
using namespace std;

// Definition of used types.
template<typename T> struct dynamic_array {};
template<typename T> struct uvector {};
struct A : public uvector<double> { typedef uvector<double> parent; };
struct C : public A { typedef A parent; };
struct D : public std::array<double,5> { typedef std::array<double,5> parent; };
template<typename T> struct B : public uvector<T> { typedef uvector<T> parent; };
template<typename T> struct container_reference {};

// Catches 'false', A, C, D  ======== HERE IT IS !!! ========
template<typename C> struct accepted_dense_vector
{
    template<typename C1 = C>
    static typename std::enable_if<accepted_dense_vector<typename C1::parent>::value, std::true_type>::type helper(const C&);
    static std::false_type helper(...);

    constexpr static bool value = decltype(helper(std::declval<C>()))::value;
};
// Catches C<T>
template<template<typename> class C, typename T>
struct accepted_dense_vector<C<T>>
{
    constexpr static bool value = std::is_arithmetic<T>::value &&
        (std::is_base_of<uvector<T>, C<T>>::value || std::is_base_of<dynamic_array<T>, C<T>>::value);
};
// Catches std::array<T,S>
template<typename T, size_t S>
struct accepted_dense_vector<std::array<T,S>>
    { constexpr static bool value = std::is_arithmetic<T>::value; };

// Catches container_reference<C>
template<typename C>
struct accepted_dense_vector<container_reference<C>>
    { constexpr static bool value = accepted_dense_vector<C>::value; };

int main()
{ // Tests!
    cout << accepted_dense_vector<std::array<double, 5>>::value << endl;
    cout << accepted_dense_vector<uvector<double>>::value << endl;
    cout << accepted_dense_vector<A>::value << endl;
    cout << accepted_dense_vector<D>::value << endl;
    cout << accepted_dense_vector<B<int>>::value << endl;
    cout << accepted_dense_vector<container_reference<uvector<double>>>::value << endl;
    cout << accepted_dense_vector<int>::value << endl;

    return 0;
}
4

1 に答える 1