You can use a static statement. Boost provides one .
Maybe something like:
#include <boost/type_traits.hpp>
#include <boost/static_assert.hpp>
template <typename T>
class my_string
{
public:
private:
BOOST_STATIC_ASSERT((boost::is_same<T, char>::value ||
boost::is_same<T, wchar_t>::value));
};
int main(void)
{
my_string<char> chstr;
my_string<wchar_t> wstr;
my_string<int> istr;
}
If you cannot use Boost, you can easily remake static-assert and is_same:
template <bool Predicate>
struct STATIC_ASSERT_FAILURE;
template <>
struct STATIC_ASSERT_FAILURE<true> {};
template <unsigned TestResult>
struct static_assert {};
#define STATIC_ASSERT(x) typedef static_assert< \
sizeof(STATIC_ASSERT_FAILURE<(x)>)> \
_static_assert_test_
template <typename T, typename U>
struct is_same
{
static const bool value = false;
};
template <typename T>
struct is_same<T, T>
{
static const bool value = true;
};
template <typename T>
class my_string
{
public:
private:
STATIC_ASSERT((is_same<T, char>::value || is_same<T, wchar_t>::value));
};
int main(void)
{
my_string<char> chstr;
my_string<wchar_t> wstr;
my_string<int> istr;
}
, , . , , __COUNTER__ .
GCC 4.4, Visual Studio 2008.