I can write a constexpr function that performs type deduction but does not use the object passed to it:
template <int N>
struct Foo
{
static const int value = N;
};
template <typename T>
constexpr int get_value(T const &)
{
return T::value;
}
void huey()
{
Foo<3> three;
static_assert(get_value(three) == 3, ":(");
}
However, if the argument to get_value is the result of some other operation, this approach fails:
template <int N>
Foo<N + 1> increase(Foo<N> const &)
{
return {};
}
void dewey()
{
Foo<6> six;
static_assert(get_value(increase(six)) == 7, ":(");
}
The compiler (rightfully) complains that increase(six) is not a constant expression. I can fix this like this:
template <typename T>
constexpr int get_value2()
{
return T::value;
}
void louie()
{
Foo<4> four;
static_assert(get_value2<decltype(increase(four))>() == 5, ":(");
}
but I do not like the extra decltype-gymnastics. I could introduce a macro:
#define GET_VALUE(x) get_value2<decltype(x)>()
but I would like to avoid macros, if possible. Is there any way to allow the convenient syntax get_value(some_function(some_object)) without macros?
increase() needs to be constexpr too
template <int N>
struct Foo
{
static const int value = N;
};
template <typename T>
constexpr int get_value(T const &)
{
return T::value;
}
void huey()
{
Foo<3> three;
static_assert(get_value(three) == 3, ":(");
}
template <int N>
constexpr Foo<N + 1> increase(Foo<N> const &)
{
return {};
}
void dewey()
{
Foo<6> six;
static_assert(get_value(increase(six)) == 7, ":(");
}
Related
I need to have a template class where each object adds itself to a vector and based on the template type parameter(allowed only: string, int, float) add to the corresponding container. I need a way to have compile time checks for the type and based on the check add to the corresponding container and if the type is not one of the allowed types compile time error should be emitted.
Example: code
vector<myClass<int>*> intVec;
vector<myClass<float>*> floatVec;
vector<myClass<string>*> stringVec;
template<typename T>
struct myClass
{
myClass()
{
/*pseudo code
if(T == int) {
intVec.push_back(this);
}
else if(T == float) {
floatVec.push_back(this);
}
else if(T == string){
stringVec.push_back(this);
}
else {
// error
}
*/
}
T value;
}
How can I achieve this ?
In C++17 and later, you can use if constexpr and std::is_same_v, eg:
#include <type_traits>
template<typename T>
struct myClass
{
myClass()
{
if constexpr (std::is_same_v<T, int>) {
m_intVec.push_back(this);
}
else if constexpr (std::is_same_v<T, float>) {
m_floatVec.push_back(this);
}
else if constexpr (std::is_same_v<T, std::string>){
m_stringVec.push_back(this);
}
else {
// error
}
}
T value;
};
In earlier versions, you can use either template specialization or SFINAE instead, eg:
// via specialization
template<typename T>
struct myClass
{
};
template<>
struct myClass<int>
{
myClass()
{
m_intVec.push_back(this);
}
int value;
};
template<>
struct myClass<float>
{
myClass()
{
m_floatVec.push_back(this);
}
float value;
};
template<>
struct myClass<std::string>
{
myClass()
{
m_stringVec.push_back(this);
}
std::string value;
};
// via SFINAE
#include <type_traits>
template<typename T>
struct myClass
{
template<typename U = T, std::enable_if<std::is_same<U, int>::value, int>::type = 0>
myClass()
{
m_intVec.push_back(this);
}
template<typename U = T, std::enable_if<std::is_same<U, float>::value, int>::type = 0>
myClass()
{
m_floatVec.push_back(this);
}
template<typename U = T, std::enable_if<std::is_same<U, std::string>::value, int>::type = 0>
myClass()
{
m_stringVec.push_back(this);
}
T value;
};
Use specialization and a helper function, e.g.
template<typename T>
struct myClass;
inline std::vector<myClass<int>*> intVec;
inline std::vector<myClass<float>*> floatVec;
inline std::vector<myClass<std::string>*> stringVec;
template<typename T>
void add(myClass<T>*);
template<>
void add(myClass<int>* p) {
intVec.push_back(p);
}
template<>
void add(myClass<float>* p) {
floatVec.push_back(p);
}
template<>
void add(myClass<std::string>* p) {
stringVec.push_back(p);
}
template<typename T>
struct myClass
{
myClass()
{
add(this);
}
T value;
};
in addition to existing answers, you can also do it with normal function overloading
template<typename T>
struct myClass;
inline std::vector<myClass<int>*> intVec;
inline std::vector<myClass<float>*> floatVec;
inline std::vector<myClass<std::string>*> stringVec;
/* optional
template<typename T>
constexpr bool always_false = false;
template<typename T>
void add(myClass<T>*) {
static_assert(always_false<T>,"unsupported T");
}
*/
void add(myClass<int>* p) {
intVec.push_back(p);
}
void add(myClass<float>* p) {
floatVec.push_back(p);
}
void add(myClass<std::string>* p) {
stringVec.push_back(p);
}
template<typename T>
struct myClass
{
myClass()
{
add(this);
}
T value;
};
I have a function with a non-type template parameter of type int, like so:
template <int N>
int foo() { /*...*/ }
I would like to unit test this function for all values of N from 0 to 32. I have a function int expected(int n) that takes the same N value and returns the expected value. Effectively, I want:
if (foo<0>() != expected(0)) { /* fail... */ }
if (foo<1>() != expected(1)) { /* fail... */ }
if (foo<2>() != expected(2)) { /* fail... */ }
// 30 more lines
I don't want to write out all 33 test cases by hand, and I can't easily use a runtime loop because N is compile time.
How can I get the compiler to generate the test cases for me in a simple way, without BOOST_PP_REPEAT-style tricks or code generation, in C++11?
You can write a recursive function template with full specialization to perform the test. e.g.
template <int N>
void test() {
test<N-1>();
if (foo<N>() != expected(N)) { /* fail... */ }
}
template <>
void test<-1>() {
// do nothing
}
and run it like
test<32>();
In c++14 you can do something like this
#include <type_traits>
template <int beg, int end> struct static_for {
template <typename Fn> void operator()(Fn const& fn) const {
if (beg < end) {
fn(std::integral_constant<int, beg>());
static_for<beg + 1, end>()(fn);
}
}
};
template <int n> struct static_for<n, n> {
template <typename Fn> void operator()(Fn const& fn) const {}
};
template <int N> int foo() { /*...*/
return N;
}
int main() {
static_for<0, 32>()([&](auto i) {
if (foo<i>() != i) { /* fail... */
}
});
return 0;
}
Here's a method:
template<int N>
void f();
template<int... N>
void g(std::index_sequence<N...>)
{
(f<N>(), ...);
}
Which can be called like so:
g(std::make_index_sequence<33>());
Edit:
Here's version that actually checks if the tests completed successfully:
template<int N>
int f();
int expected(int n);
template<int... N>
bool g(std::index_sequence<N...>)
{
return ((f<N>() == expected(N)) && ...);
}
Which is used like:
g(std::make_index_sequence<33>()); // true if all tests are sucessful, false otherwise
A possible C++14 solution, that "simulate" the C++17 template-folding and interrupt the f<N> != expected(N) at first failure
template <int N>
void f ();
template <int ... Is>
void g (std::integer_sequence<int, Is...>)
{
using unused = int[];
bool ret { false };
(void)unused { 0, (ret ? 0 : (ret = (f<Is>() != expected(Is)), 0))... };
}
callable as follows
g(std::make_integer_sequence<33>());
For a C++11 solution, you need a substitute for std::make_integer_sequence/std::integer_sequence that are available only from C++14.
I'm working on a C++11 wrapper around a C api. The C api offers a bunch of getters for various types, with a different name for each type. Values are retrieved by array of a given size, known at compilation.
I want to give the type and the array size by template, to call the right function.
#include <string>
#include <iostream>
template <typename T>
struct make_stop {
constexpr static bool value = false;
};
class Foo
{
public:
Foo() : i(42) {}
template<typename T, size_t n>
T get();
private:
int i = 0;
};
template<typename T, size_t n>
T Foo::get() { static_assert(make_stop<T>::value); return T(); }
template<int, size_t n>
int Foo::get() { return i + n; }
int main() {
Foo foo;
int i = foo.get<int, 4>();
double f = foo.get<double, 2>();
return 0;
}
But it fails to match the right function
main.cpp:26:5: error: no declaration matches 'int Foo::get()'
int Foo::get() { return i + n; }
^~~
main.cpp:15:7: note: candidate is: 'template<class T, long unsigned int n> T Foo::get()'
T get();
its a bit vauge from your question, but assuming you are wanting to index into some c- arrays and return the value at I you can't specialize function templates like you want, but you can use some tags instead, something like..
class Foo
{
public:
Foo() : is{1,2,3,4,5,6,7,8,9,10},ds{1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.1} {}
template <typename T> struct type_c{};
template <size_t I> struct int_c{};
template<typename T,size_t I>
auto get()
{ return get_impl(type_c<T>(),int_c<I>()); }
private:
template <size_t I>
auto get_impl(type_c<int>,int_c<I>)
{ return is[I]; }
template <size_t I>
auto get_impl(type_c<double>,int_c<I>)
{ return ds[I]; }
int is[10];
double ds[10];
};
int main() {
Foo foo;
int i = foo.get<int,0>();
double d = foo.get<double,2>();
std::cout << i << " " << d << std::endl;
return 0;
}
Demo
If I understood you correctly you want to partially specialize get for T. Unfortunately partial specialization for methods is not allowed by the standard. You can however get around this with a static method on a class templated by T and specializing the class.
Like this:
template <class T> struct Foo_helper;
struct Foo
{
Foo() : i{42} {}
template<class T, std::size_t N>
T get()
{
return Foo_helper<T>::template get<N>(*this);
}
int i = 0;
};
template <class T> struct Foo_helper {};
// specialize Foo_helper for each type T you wish to support:
template <> struct Foo_helper<int>
{
template <std::size_t N>
static int get(const Foo& foo) { return foo.i + N; }
};
template <> struct Foo_helper<double>
{
template <std::size_t N>
static double get(const Foo& foo) { return foo.i + N; }
};
int main()
{
Foo foo{};
int i = foo.get<int, 4>();
double d = foo.get<double, 2>();
}
I am currently creating arithmetic operators libraries for high level synthesis.
For this, I am also creating a library to manipulate bits and bit vectors like it would be done in VHDL. To make my libraries synthesizable, nearly everything must be resolved at compile time.
However, I have an issue with loops.
Indeed, I would like to be able to write things like that:
const int N = 5;
for(int i = 0; i < N-2; i++) {
x.bit<i+2>() = x.bit<i>();
}
Of course, it does not compile since i is a variable and not a constant determined at compile time.
However, N being a constant, this code is strictly equivalent to:
x.bit<2>() = x.bit<0>();
x.bit<3>() = x.bit<1>();
x.bit<4>() = x.bit<2>();
which compiles and works perfectly.
Is there a way to make the compiler (gcc in my case) unroll the loop since N is constant? Or to define a macro or a constexpr which could do it with a clean syntax? This would be the equivalent of for generate in VHDL.
While constexpr has got much more powerful in C++14/17 it is not yet possible to mix this kind of compile time / template code with an ordinary loop. There is some talk of introducing a construct that might enable that in a future version of C++. For now you have a few choices, either recursive calls to a function with an integer template argument or probably simpler in this case a C++17 fold expression. You could also use C++11 variadic template expansion to get a similar result to fold expressions in this example, though fold expressions are more powerful.
Just saw your comment about being stuck with C++11, you're probably better off using the recursive function approach I think. I've added that approach to the example.
If you were able to use C++14 you might also want to consider moving entirely into constexpr function / type land so your bit<I>() function would not be templated but would be just a constexpr function bit(i). You could then use normal functions and loops. Given the C++11 restrictions on constexpr functions that is probably less useful in your case however. I've added an example using that approach.
#include <iostream>
#include <utility>
template <size_t N>
struct bits {
bool bs[N];
template <size_t I>
constexpr const bool& bit() const {
return bs[I];
}
template <size_t I>
constexpr bool& bit() {
return bs[I];
}
constexpr bool bit(int i) const { return bs[i]; }
constexpr void bit(int i, bool x) { bs[i] = x; }
};
// Using C++17 fold expressions
template <size_t N, size_t... Is>
constexpr bits<N> set_bits_helper(bits<N> x, std::index_sequence<Is...>) {
((x.bit<Is + 2>() = x.bit<Is>()), ...);
return x;
}
template <size_t N>
constexpr bits<N> set_bits(bits<N> x) {
return set_bits_helper(x, std::make_index_sequence<N - 2>{});
}
// Using recursive template function, should work on C++11
template <size_t I, size_t N>
constexpr bits<N> set_bits_recursive_helper(bits<N> x, std::integral_constant<size_t, I>) {
x.bit<N - I>() = x.bit<N - I - 2>();
return set_bits_recursive_helper(x, std::integral_constant<size_t, I - 1>{});
}
template <size_t N>
constexpr bits<N> set_bits_recursive_helper(bits<N> x, std::integral_constant<size_t, 0>) { return x; }
template <size_t N>
constexpr bits<N> set_bits_recursive(bits<N> x) {
return set_bits_recursive_helper(x, std::integral_constant<size_t, N - 2>{});
}
// Using non template constexpr functions
template <size_t N>
constexpr bits<N> set_bits_constexpr(bits<N> x) {
for (int i = 0; i < N - 2; ++i) {
x.bit(i + 2, x.bit(i));
}
return x;
}
// Test code to show usage
template <size_t N>
void print_bits(const bits<N>& x) {
for (auto b : x.bs) {
std::cout << b << ", ";
}
std::cout << '\n';
}
void test_set_bits() {
constexpr bits<8> x{ 1, 0 };
print_bits(x);
constexpr auto y = set_bits(x);
static_assert(y.bit<2>() == x.bit<0>());
print_bits(y);
}
void test_set_bits_recursive() {
constexpr bits<8> x{ 1, 0 };
print_bits(x);
constexpr auto y = set_bits_recursive(x);
static_assert(y.bit<2>() == x.bit<0>());
print_bits(y);
}
void test_set_bits_constexpr() {
constexpr bits<8> x{ 1, 0 };
print_bits(x);
constexpr auto y = set_bits_constexpr(x);
static_assert(y.bit<2>() == x.bit<0>());
print_bits(y);
}
int main() {
test_set_bits();
test_set_bits_recursive();
test_set_bits_constexpr();
}
Also without std::integer_sequence (but I suggest to implement a substitute and use it), in C++11 you can use template partial specialization.
I mean that you can implement something like
template <int I, int Sh, int N>
struct shiftVal
{
template <typename T>
static int func (T & t)
{ return t.template bit<I+Sh>() = t.template bit<I>(),
shiftVal<I+1, Sh, N>::func(t); }
};
template <int I, int Sh>
struct shiftVal<I, Sh, I>
{
template <typename T>
static int func (T &)
{ return 0; }
};
and your cycle become
shiftVal<0, 2, N-2>::func(x);
The following is a full working example
#include <array>
#include <iostream>
template <std::size_t N>
struct foo
{
std::array<int, N> arr;
template <int I>
int & bit ()
{ return arr[I]; }
};
template <int I, int Sh, int N>
struct shiftVal
{
template <typename T>
static int func (T & t)
{ return t.template bit<I+Sh>() = t.template bit<I>(),
shiftVal<I+1, Sh, N>::func(t); }
};
template <int I, int Sh>
struct shiftVal<I, Sh, I>
{
template <typename T>
static int func (T &)
{ return 0; }
};
int main ()
{
foo<10U> f { { { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 } } };
for ( auto const & i : f.arr )
std::cout << i << ' ';
std::cout << std::endl;
shiftVal<0, 2, 10-2>::func(f);
for ( auto const & i : f.arr )
std::cout << i << ' ';
std::cout << std::endl;
}
Nobody else produce an example based on a C++11 simulation of std::integer_sequence (as suggested by W.F., Passer By and Sopel and the simpler solution, IMHO) so I propose the following one (of std::index_sequence and std::make_index_sequence in reality: simulate std::integer_sequence is more complicated)
template <std::size_t ...>
struct indexSequence
{ };
template <std::size_t N, std::size_t ... Next>
struct indexSequenceHelper : public indexSequenceHelper<N-1U, N-1U, Next...>
{ };
template <std::size_t ... Next>
struct indexSequenceHelper<0U, Next ... >
{ using type = indexSequence<Next ... >; };
template <std::size_t N>
using makeIndexSequence = typename indexSequenceHelper<N>::type;
So a function (with function helper) to reproduce the asked loop can be written as
template
void shiftValHelper (T & t, indexSequence<Is...> const &)
{
using unused = int[];
(void)unused { 0,
(t.template bit<Is+Sh>() = t.template bit<Is>(), 0)... };
}
template <std::size_t Sh, std::size_t N, typename T>
void shiftVal (T & t)
{ shiftValHelper<Sh>(t, makeIndexSequence<N>{}); }
and called ad follows
shiftVal<2, N-2>(x);
The following is a full working example
#include <array>
#include <iostream>
template <std::size_t ...>
struct indexSequence
{ };
template <std::size_t N, std::size_t ... Next>
struct indexSequenceHelper : public indexSequenceHelper<N-1U, N-1U, Next...>
{ };
template <std::size_t ... Next>
struct indexSequenceHelper<0U, Next ... >
{ using type = indexSequence<Next ... >; };
template <std::size_t N>
using makeIndexSequence = typename indexSequenceHelper<N>::type;
template <std::size_t N>
struct foo
{
std::array<int, N> arr;
template <std::size_t I>
int & bit ()
{ return arr[I]; }
};
template <std::size_t Sh, typename T, std::size_t ... Is>
void shiftValHelper (T & t, indexSequence<Is...> const &)
{
using unused = int[];
(void)unused { 0,
(t.template bit<Is+Sh>() = t.template bit<Is>(), 0)... };
}
template <std::size_t Sh, std::size_t N, typename T>
void shiftVal (T & t)
{ shiftValHelper<Sh>(t, makeIndexSequence<N>{}); }
int main ()
{
foo<10U> f { { { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 } } };
for ( auto const & i : f.arr )
std::cout << i << ' ';
std::cout << std::endl;
shiftVal<2, 10-2>(f);
for ( auto const & i : f.arr )
std::cout << i << ' ';
std::cout << std::endl;
}
Consider the following code:
class Helper {
public:
template<typename taResult, typename taParam> static taResult Cast(const taParam par);
};
template<> inline __m256d Helper ::Cast(const __m256i par) {
return _mm256_castsi256_pd(par);
}
template<> inline __m256i Helper ::Cast(const __m256d par) {
return _mm256_castpd_si256(par);
}
I want to add to the Helper a function to handle casts where the parameter and the return types are equals. All my attempts to specialize/overload so far have failed with different compilation errors.
Something like the following in the class body:
template<typename T> static T Cast(const T par) {
return par;
}
You cannot partial specialize function, and your overload would be ambiguous.
You can add class which you can partial specialize though:
template <typename To, typename From> struct CastImpl;
template <typename T> struct CastImpl<T, T>
{
T operator()(T t) const { return t; }
};
template <> struct CastImpl<__m256d, __m256i>
{
__m256d operator()(__m256i t) const { return _mm256_castsi256_pd(t); }
};
template <> struct CastImpl<__m256i, __m256d>
{
__m256i operator()(__m256d t) const { return _mm256_castpd_si256(t); }
};
and then
class Helper {
public:
template<typename taResult, typename taParam>
static taResult Cast(const taParam par)
{
return CastImpl<taResult, taParam>{}(par);
}
};
No you can not, because that would be an attempt to partially specialize a function, which is not allowed. Instead, you'd have to use an intermediate template class, which than can be specialized.
I can provide example if needed.
You can use a helper class/struct template to implement Helper::Cast.
Here's a simple program that has uses a few shortcuts to demonstrate the concept.
using __m256d = double;
using __m256i = int;
template<typename taResult, typename taParam> struct RealHelper;
class Helper
{
public:
template<typename taResult, typename taParam> static taResult Cast(const taParam par)
{
return RealHelper<taResult, taParam>::doit(par);
}
private:
};
template <> struct RealHelper<__m256d, __m256i>
{
inline static __m256d doit(const __m256i par)
{
// return _mm256_castsi256_pd(par);
return par;
}
};
template <> struct RealHelper<__m256i, __m256d>
{
inline static __m256i doit(const __m256d par)
{
// return _mm256_castpd_si256(par);
return par;
}
};
template <typename T> struct RealHelper<T, T>
{
inline static T doit(const T par)
{
return par;
}
};
int main()
{
auto v1 = Helper::Cast<int, double>(10);
auto v2 = Helper::Cast<double, int>(20);
auto v3 = Helper::Cast<int, int>(30);
auto v4 = Helper::Cast<double, double>(40);
}
I want to add to the Helper a function to handle casts where the parameter and the return types are equals.
What about using SFINAE to enable/disable a Cast() version according the value of std::is_same<taResult, taParam>::value ?
A simplified example
#include <iostream>
#include <type_traits>
struct Helper
{
template <typename taR, typename taP>
static std::enable_if_t<false == std::is_same<taR, taP>::value, taR>
Cast (taP const & par)
{ std::cout << "different Cast" << std::endl; return {}; }
template <typename taR, typename taP>
static std::enable_if_t<true == std::is_same<taR, taP>::value, taR>
Cast (taP const & par)
{ std::cout << "equal Cast" << std::endl; return par; }
};
template <>
int Helper::Cast<int, long> (long const & par)
{ std::cout << "int/long Cast" << std::endl; return {}; }
template <>
long Helper::Cast<long, int> (int const & par)
{ std::cout << "long/int Cast" << std::endl; return {}; }
int main()
{
Helper::template Cast<int>(0); // print "equal Cast"
Helper::template Cast<int>(0L); // print "int/log Cast"
Helper::template Cast<long>(0); // print "long/int Cast"
Helper::template Cast<long>("foo"); // print "different Cast"
}