0

Suppose I have a class

template <typename T>
struct Foo {
    T data;
};

where I want to enforce the fact that T is a known templated type. For instance, suppose that I want T to be a std::vector of something. Now one way to handle this problem would be to redefine the class so that the template parameter is also the template parameter of the data function:

template <typename S>
struct Foo {
    std::vector<S> data;
};

I do not want to do that. In this specific case, it feels more natural to require the user to construct a foo object like this:

Foo<T<S>> f;

which, in the case where we want T to be a vector, looks like

Foo<std::vector<S>> f;

Is there anyway to keep the first form of the class, which requiring that T be of a specific class? Perhaps a static_assert?

bremen_matt
  • 6,902
  • 7
  • 42
  • 90

1 Answers1

0

Got it

template <typename T>
struct Foo {
    static_assert( std::is_same<T,std::vector<T::value_type>>::value, "T is not a vector you idiot" );
};
bremen_matt
  • 6,902
  • 7
  • 42
  • 90