set a specific member with template parameter - c++

let's say I have this:
struct myStruct {
int A;
int B;
}
Is it possible to set a specific member via a template parameter like this?
void setTo10<?? member>(myStruct& obj) {
obj.member = 10;
}
//usage:
setTo10<"member A">(obj);
I know it's possible with a macro but how about a template?
thanks

Something like this?
struct myStruct {
int A;
int B;
};
template <typename T, typename V>
void set(T& t, V T::*f, V v)
{ t.*f = v; }
int main()
{
myStruct m;
set(m, &myStruct::A, 10);
std::cout << m.A << '\n';
}

This solution allows to select a member via a compile-time index (which could be computed via another compile-time expression):
struct myStruct {
int A;
int B;
};
template <int n1, int n2>
struct SetOnEqual
{
static void set(int& var, int val)
{} // default: do nothing
};
template<int n>
struct SetOnEqual<n, n>
{
static void set(int& var, int val)
{
var = val;
}
};
template <int n>
void setTo10(myStruct& s)
{
SetOnEqual<n,0>::set(s.A, 10);
SetOnEqual<n,1>::set(s.B, 10);
}
Then the following code
#include <stdio.h>
int main()
{
myStruct s;
s.A = s.B = 0;
setTo10<0>(s); // sets s.A
printf("s=(%d,%d)\n", s.A, s.B);
setTo10<1>(s); // sets s.B
printf("s=(%d,%d)\n", s.A, s.B);
return 0;
}
gives the output
s=(10,0)
s=(10,10)

Related

How can I use iterator values in templates?

Consider the following code:
#include <iostream>
enum class E
{
A,
B
};
template<E e> int f();
template<> int f<E::A>(){ return 1; }
template<> int f<E::B>(){ return 2; }
int main()
{
for( const E i : {E::A, E::B} )
{
std::cout << f<i>() << "\n";
}
}
This fails to compile, because i is not initialised with a constant expression. Is it possible make this idea of a code work?
This is very related: Why isn't a for-loop a compile-time expression?.
i in your loop is not a constant expression. However, by stealing from this answer one can make your code call f<i> inside the loop. It is not directly what you asked for because the proposed solution is for size_t index based loops instead of iterators, but it does call f<i> for all values of the enum:
#include <iostream>
#include <utility>
// your code
enum class E
{
A,
B
};
template<E e> int f();
template<> int f<E::A>(){ return 1; }
template<> int f<E::B>(){ return 2; }
// https://stackoverflow.com/a/47563100/4117728
template<std::size_t N>
struct num { static const constexpr auto value = N; };
template <class F, std::size_t... Is>
void for_(F func, std::index_sequence<Is...>)
{
using expander = int[];
(void)expander{0, ((void)func(num<Is>{}), 0)...};
}
template <std::size_t N, typename F>
void for_(F func)
{
for_(func, std::make_index_sequence<N>());
}
// emulate constexpr for (size_t i=0;i<2;++i) f<i>();
int main()
{
for_<2>([&] (auto i) {
std::cout << f<static_cast<E>(decltype(i)::value)>();
});
}
Output:
12
Probably there is a simpler solution for your actual problem. The answer to your literal question is: No. You cannot call f<i> when i is not a compile time constant. For detailed explanation I refer you to the above mentioned q&a.
The compiler is not able to deduce i in the call to f<i>(). This is because compilation happens before runtime, and the value of i is only available during runtime. You can work around this by adding a translator.
#include <iostream>
enum class E
{
A,
B
};
template<E e> int f();
template<> int f<E::A>(){ return 1; }
template<> int f<E::B>(){ return 2; }
int bar(E e)
{
int retval = 0;
switch(e)
{
case E::A:
{
return f<E::A>();
break;
}
case E::B:
{
return f<E::B>();
break;
}
default:
{
break;
}
}
return retval;
}
int main()
{
for( const E i : {E::A, E::B} )
{
std::cout << bar(i) << "\n";
}
return 0;
}

Size of std::array in class template depending on template parameter

I have a the following class template
template<int N>
constexpr int arraySize() { return arraySize<N-1>() + N; }
template<>
constexpr int arraySize<0>() { return 0; }
template<int C>
class MyClass {
public:
std::array<int, arraySize<C>()> arr;
};
int main() {
MyClass<3> cls;
std::cout << cls.arr.size() << std::endl; // Output: 6
}
Everything works but I would like to have calculateArraySize<N>() as a member function. I've tried the following:
template<int C>
class MyClass {
public:
static constexpr int arraySize();
std::array<int, MyClass<C>::arraySize()> arr;
};
template<int C>
constexpr int MyClass<C>::arraySize(){ return MyClass<C-1>::arraySize() + C; }
template<>
constexpr int MyClass<0>::arraySize() { return 0; }
Results in the following error:
fatal error: recursive template instantiation exceeded maximum
depth of 1024
std::array::arraySize()> arr;
template<int C>
class MyClass {
public:
template<int N>
static constexpr int arraySize();
std::array<int, MyClass::arraySize<C>()> arr;
};
template<int C>
template<int N>
constexpr int MyClass<C>::arraySize(){ return MyClass::arraySize<N-1>() + N-1; }
template<int C>
template<>
constexpr int MyClass<C>::arraySize<0>() { return 0; }
Gives the following error:
tmp.cc:19:27: error: cannot specialize (with 'template<>') a member of an
unspecialized template
constexpr int MyClass::arraySize<0>() { return 0; }
Is it possible to achieve the desired behaviour? Solutions using C++14/C++17 features (I guess it should be possible usinn if-constexpr) are welcomed but won't solve my particular problem since only C++11 is available.
You can move the function into the class and the specialize the entire class for the base case. That looks like:
template<int C>
class MyClass {
public:
static constexpr int arraySize(){ return MyClass<C-1>::arraySize() + C; }
std::array<int, MyClass<C>::arraySize()> arr;
};
template<>
class MyClass<0> {
public:
static constexpr int arraySize(){ return 0; }
};
int main() {
MyClass<3> cls;
std::cout << cls.arr.size() << std::endl; // Output: 6
}
Live Example
You can also use a member variable instead of a member function.
template <int C>
class MyClass {
public:
static constexpr int array_size = MyClass<C-1>::array_size + C;
std::array<int, array_size> arr;
};
template <>
class MyClass<0> {
public:
static constexpr int array_size = 0;
};

Function pointers with templates

I want a function pointer that points to a function that takes as argument a class with a template parameter (see main). I believe I'm close to the right syntax, but I receive the compilation error: "a template declaration cannot appear at block scope".
#include <iostream>
#include <array>
template<int N>
class NumberHolder
{
public:
NumberHolder();
int x_;
};
template<int N>
NumberHolder<N>::NumberHolder() : x_(N) {}
template<int N, int M>
void add(NumberHolder<N>& nh)
{
nh.x_ += M;
}
template<int N, int M>
void mult(NumberHolder<N>& nh)
{
nh.x_ *= M;
}
int main()
{
NumberHolder<3> nh;
//using f_ptr = void(*)(NumberHolder<3>&); // Compiles
template<int N> using f_ptr = void(*)(NumberHolder<N>&); // Doesn't compile
std::array<f_ptr, 2> operations;
operations[0] = &add<3, 41>;
operations[1] = &mult<3, 8>;
for (int i = 0; i < operations.size(); ++i)
{
operations[i](nh);
}
std::cout << nh.x_ << std::endl;
return 0;
}
You have to move your template alias outside of local scope:
template <int N> using f_ptr = void (*)(NumberHolder<N>&);
int main()
{
const std::array<f_ptr<3>, 2> operations {&add<3, 41>, &mult<3, 8>};
NumberHolder<3> nh;
for(const auto& operation : operations) {
operation(nh);
}
std::cout << nh.x_ << std::endl;
return 0;
}
Live example.

Static member function pointer as template argument

I get this compile error with the latest VC++ compiler (Nov 2012 CTP) when using static member function pointer as template argument:
error C2027: use of undefined type 'wrapper<int (int,int),int A::f1(int,int)>'
But when using free function, everything works ok.
I looked up some similar bugs in g++( pointer to static member function is "invalid" as a template argument for g++ ), but there it explicitly states that argument is invalid. What is so different about static functions?
I'm casting the function to void(*)(void) because construct like <typename T_Ret, typename... T_Args, T_Ret(*)(T_Args...)> don't compile for some other urelated reasons.
struct A
{
static int f1(int a, int b)
{
return a + b;
}
};
int f2(int a, int b)
{
return a + b;
}
template <typename Sig, void(*fnc)(void)>
struct wrapper;
template <void(*fnc)(void), typename T_Ret, typename... T_Args>
struct wrapper<T_Ret (T_Args...), fnc>
{
static bool apply()
{
// get some ints here
int a = 1;
int b = 2;
typedef T_Ret (fnc_ptr*)(T_Args...);
int res = ( (fnc_ptr)fnc )(a, b);
// do smth with result
res;
return true; // or false
}
};
int main()
{
bool res;
res = wrapper<decltype(A::f1), (void(*)(void))A::f1>::apply(); // error
res = wrapper<decltype(f2), (void(*)(void))f2>::apply(); // compiles ok
return 0;
}
EDIT:
Ok, I narrowed the issue to decltype.
When I write the type explicitly, everything works:
res = wrapper<int(int, int), (void(*)(void))A::f1>::apply(); // compiles ok
EDIT:
Looks like it's a compiler bug: http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Core-C-/STLCCSeries6#c634886322325940618
Workaround:
Change decltype(A::f1) to decltype(&A::f1) which changed its output from int(int, int) to int (__cdecl *)(int,int). And change
template <void(*fnc)(void), typename T_Ret, typename... T_Args>
struct wrapper<T_Ret (T_Args...), fnc>
to
template <void(*fnc)(void), typename T_Ret, typename... T_Args>
struct wrapper<T_Ret (*)(T_Args...), fnc>
Working code:
struct A
{
static int f1(int a, int b)
{
return a + b;
}
};
template <typename Sig, void(*fnc)(void)>
struct wrapper;
template <void(*fnc)(void), typename T_Ret, typename... T_Args>
struct wrapper<T_Ret (*)(T_Args...), fnc>
{
static bool apply()
{
// get some ints here
int a = 1;
int b = 2;
typedef T_Ret (*fnc_ptr)(T_Args...);
int res = ( (fnc_ptr)fnc )(a, b);
// do smth with result
res;
return true; // or false
}
};
int main()
{
bool res;
res = wrapper<decltype(&A::f1), (void(*)(void))A::f1>::apply();
return 0;
}
You could try something like this:
#include <iostream>
using namespace std;
struct A
{
static int f1(int a, int b)
{
return a + b;
}
};
int f2(int a, int b)
{
return a + b;
}
template <typename T, T X>
struct wrapper
{
template <typename... Args>
static bool value(Args... blargs)
{
return X(blargs...) == 3;
}
};
int main()
{
bool res;
res = wrapper<decltype(&A::f1), &A::f1>::value(1,2);
cout << res << endl;
return 0;
}
But seriously, this is so much easier:
#include <iostream>
using namespace std;
int main()
{
bool res;
res = A::f1(a, b) == 3;
cout << res << endl;
return 0;
}

Is there a way to initialize a variable with a value based on the template type

I have a template class. The class has a private member variable which I like to have preset to a certain value which differs for each template type.
I was thinking about different constructors for different types, but since the constructor has no parameters I have no idea how to do it.
Is it possible anyway ?
Thanks,
Use a traits template and specialize it with the value. something like:
template <typename T, typename Traits = MyTraits<T> >
class MyClass {
public:
int Foo ()
{
return Traits::Value;
}
};
template <>
class MyTraits<SomeClass>
{
public:
static int Value = 1;
};
template <>
class MyTraits<AnotherClass>
{
public:
static int Value = 2;
};
You can do it with a specialization on the type, the easiest form is:
#include <iostream>
template <typename T>
struct make {
static int value() {
return -1; // default
}
};
template <>
struct make<int> {
static int value() {
return 1;
}
};
template <>
struct make<double> {
static int value() {
return 2;
}
};
template <typename T>
struct foo {
const int val;
foo() : val(make<T>::value()) {}
};
int main() {
std::cout << foo<int>().val << ", " << foo<double>().val << "\n";
}
But you could also arrange it as an overload:
#include <iostream>
int value(void *) {
return -1; // default
}
int value(double *) {
return 2;
}
int value (int *) {
return 1;
}
template <typename T>
struct foo {
const int val;
foo() : val(value(static_cast<T*>(nullptr))) {}
};
int main() {
std::cout << foo<int>().val << ", " << foo<double>().val << "\n";
}
You could put the mapping from template parameters to values into a helper class, giving you something like this:
template<typename T> struct foo_helper;
template<> struct foo_helper<int> { static int getValue() {return 1; } };
template<> struct foo_helper<float> { static int getValue() {return 2; } };
....
template<typename T> class foo
{
int m;
foo():m(foo_helper<T>::getValue()){}
};