Reputation: 22074
I have some templates spezialized for certain data types. Now in the x64 build the types are different, so it works fine. For a x32 build, the datatypes are the same, so I wonder if I can do a conditional compile only if the datatypes are different.
template <> const usize_t NumericColumn<int_t>::MinDefault = (usize_t)INT_MIN;
template <> const usize_t NumericColumn<int_t>::MaxDefault = (usize_t)INT_MAX;
template <> const usize_t NumericColumn<uint_t>::MinDefault = (usize_t)0;
template <> const usize_t NumericColumn<uint_t>::MaxDefault = (usize_t)UINT_MAX;
// May not work in 32 bit build, but so far we don't need this anyway and if it is
// desired, it needs to be adjusted accordingly with an ifdef WIN32/WIN64
template <> const usize_t NumericColumn<ssize_t>::MinDefault = (usize_t)LLONG_MIN;
template <> const usize_t NumericColumn<ssize_t>::MaxDefault = (usize_t)LLONG_MAX;
template <> const usize_t NumericColumn<usize_t>::MinDefault = (usize_t)0;
template <> const usize_t NumericColumn<usize_t>::MaxDefault = (usize_t)ULLONG_MAX;
In x32 usize_t
is uint_t
is int
, so I get a compiler error because the templates are instantiated twice.
I can of corse use a
#ifdef WIN64
template usize_t...
#endif
But I was wondering if I can do this in C++ with comparing the type itself, which would be cleaner IMO. If I use a static_assert
I can only generate an error, which of course is not what I need here.
Upvotes: 0
Views: 167
Reputation:
What you need is already in the standard library:
template <typename T>
const usize_t NumericColumn<T>::MinDefault = std::numeric_limits<T>::min();
template <typename T>
const usize_t NumericColumn<T>::MaxDefault = std::numeric_limits<T>::max();
As to the question you actually asked, you could use a compile-time program:
template <typename T>
const usize_t NumericColumn<T>::MinDefault = DefaultComputer<T>::min;
template <typename T>
const usize_t NumericColumn<T>::MaxDefault = DefaultComputer<T>::max;
and implement DefaultComputer
as a metaprogram to do the calculation you want, e.g. by storing a list of type-value pairs and taking the first entry with a matching type.
A maybe better way is to do the specialization based on type names you know to be distinct; e.g. assuming you are using suitable types,
template <typename T>
const usize_t NumericColumn<int32_t>::MinDefault = INT32_MIN;
template <typename T>
const usize_t NumericColumn<uint32_t>::MinDefault = UINT32_MIN;
template <typename T>
const usize_t NumericColumn<int64_t>::MinDefault = INT64_MIN;
template <typename T>
const usize_t NumericColumn<uint64_t>::MinDefault = UINT64_MIN;
Upvotes: 2
Reputation: 2822
I think you are looking for this:
enable_if<is_same<uint_t, usize_t>::value>
Upvotes: 1