I want to define additional variables in a struct based on the type in template, something like this:
template<typename CONFIG>
struct Test
{
int a;
int b;
if (std::is_same<CONFIG, MyClass>::value)
{
// additional variables if the CONFIG type is MyClass
int c;
int d;
}
// functions
void func()
{
a = 0;
b = 0;
if (std::is_same<CONFIG, MyClass>::value)
{
c = 0;
d = 0;
}
}
}
How should I do that? Thanks!
This is a use case for template specialization. You have a primary template like
template<typename CONFIG>
struct Test
{
int a;
int b;
// functions
void func()
{
a = 0;
b = 0;
}
};
and then a specialization for MyClass like
template <>
struct Test<MyClass>
{
int a;
int b;
int c;
int d;
// functions
void func()
{
a = 0;
b = 0;
c = 0;
d = 0;
}
};
A variant that follows your pseudo-code a bit more (not saying that it is a better approach than the other answer):
template<typename CONFIG>
struct TestAdditionalMembers {};
template<>
struct TestAdditionalMembers<MyClass>
{
int c;
int d;
};
template<typename CONFIG>
struct Test : TestAdditionalMembers<CONFIG>
{
int a;
int b;
// functions
void func()
{
a = 0;
b = 0;
if constexpr(std::is_same<CONFIG, MyClass>::value)
{
this->c = 0;
this->d = 0;
}
}
};
The additional members are provided through inheritance and explicit specialization of the class template. Since c and d are not dependent names, but only exist for certain template parameters, you will need to refer to them with this->c, etc. (this is always a dependent name).
Inside a function the condition needs to be checked by a if constexpr, since at-runtime check would be too late. (The code inside the if must compile even if the condition is always false.)
Based on walnuts answer, you could collect all your conditional actions and member variables in a specialization that you inherit if you want to avoid type traits and constexpr-if's.
template<typename T>
struct completions {
void func_completion() {} // does nothing
};
template<>
struct completions<MyClass> {
int c;
int d;
void func_completion() {
c = 0;
d = 0;
}
};
template<typename CONFIG>
struct Test : completions<CONFIG> {
int a;
int b;
// functions
void func() {
a = 0;
b = 0;
this->func_completion();
}
};
Related
For instance I have a function
void func(my_cont &C){
C.membA = 1;
C.membB = 2;
dosomething_with(C);
}
Also what to do in the function, if I have a Struct that does not have a member B?
This is a way to statically check for the existence of a membB member inside the template function.
template<typename T>
void func(T& C)
{
C.membA = 1;
if constexpr (requires() { C.membB; })
{
C.membB = 2;
}
}
int main()
{
struct A
{
int membA;
};
struct B
{
int membA;
int membB;
};
A a;
func(a);
B b;
func(b);
}
Another way to get functionality that differs per type:
Using template specialization, as OP requested.
struct A
{
int membA;
};
struct B
{
int membA;
int membB;
};
template<typename T> void func(T&);
template<> void func<A>(A& a) {
a.membA = 1;
}
template<> void func<B>(B& b) {
b.membA = 1;
b.membB = 2;
}
int main()
{
A a;
func(a);
B b;
func(b);
}
First of all, the code is restricted to C++11, so I cannot make use of if constexpr
Following is my sample code snippet:
class A{
public:
int a;
int b;
}
class B{
public:
int key;
int val;
}
class C{
public:
int n1;
int n2;
}
class D{
public:
int n1;
int n2;
}
class E{
public:
int n1;
int n2;
}
template<typename T>
void func1(T data) {
if (T == type(A)) { // Just pseudo template-check code
std::cout<<data.a<<data.b; //<------1
} else if (T == type (B)) { // Just pseudo template-check code
std::cout<<data.key<<data.val; //<------2
} else {
std::cout<<data.n1<<data.n2; //<------3
}
int main() {
A a;
B b;
C c;
D d;
E e;
func1(a);
func1(b);
func1(c);
func1(d);
func1(e);
return 0;
}
Currently, I get a compile-time error at,
1: B,D,E,F has no member a & b
&
2: A,D,E,F has no member key & val
&
3. A, B has no member n1 & n2
I tried using is_same() & also this, but I get same compile time error every time.
I cannot make use of C++14/C++17
How could I make use of specialized template functions?
Edited the code to highlight the need of a template.
You can use a function overload and avoid the function template altogether.
void func1(A a)
{
// Type dependent code.
}
void func1(B a)
{
// Type dependent code.
}
A function template makes sense only if there is common code for all the types for which the function call is made. If you have some code that is common to all types and some code that are type dependent, then you can use:
void func1(A a)
{
// Type dependent code.
}
void func1(B a)
{
// Type dependent code.
}
template <typename T>
void func2(T t)
{
// Type independent code.
}
template <typename T>
void func(T obj)
{
func1(obj); // Call function that uses type dependent code.
func2(obj); // Call function that uses type independent code.
}
You must write specializations of the function for the two types your want to use it with.
#include<iostream>
class A{
public:
int a;
int b;
};
class B{
public:
int key;
int val;
};
template<typename T>
void func1(T);
template<>
void func1<A>(A arg) {
std::cout<<"A"<<std::endl;
std::cout<<arg.a<<arg.b;
}
template<>
void func1<B>(B arg) {
std::cout<<"B"<<std::endl;
std::cout<<arg.key<<arg.val;
}
int main(){
A a;
func1(a);
B b;
func1(b);
}
Simple overload does the job.
template <typename T>
void func1(T data)
{
std::cout << data.n1 << data.n2;
}
void func1(A data)
{
std::cout << data.a << data.b;
}
void func1(B data)
{
std::cout << data.key << data.val;
}
https://godbolt.org/z/r7Ee6E
Tweaked a bit: https://godbolt.org/z/xxPWaE
Lets assume we have two classes
struct A
{
int x = 1;
};
struct B
{
int y = 2;
};
I want to have template that will return value of member (in a case of A I want to return value of "x", in case of B I want to return value of "y").
Example call:
const auto myVariable = f<A>();
or
A a;
const auto myVariable = f<A>(a);
I don't want to have 2 template specializations - ideally it would be one template with some kind of "if statement", but maybe it is not possible?
It may be written with C++11 (but not with C++14).
Generally how you are using templates when you have such problems - quite big template and only in one or two places you need to take values from different members - which may be deduced based of type of that variable.
PROBLEM: unnecessary it is not allowed to modify classes A and B
Why use templates at all?
int f(const A& a) { return a.x; }
int f(const B& b) { return b.y; }
Just in case you ask for the template because you want to switch between A and B at compile time...and you have a reason not to simply typedef A or B directly...
struct A
{
int x;
};
struct B
{
int y;
};
struct A1 : public A { int Get() const { return x; } };
struct B1 : public B { int Get() const { return y; } };
// Begin possible shortcut avoiding the template below:
#ifdef USE_A
typedef A1 Bar;
#endif
#ifdef USE_B
typedef B1 Bar;
#endif
// End possible shortcut.
template <class _Base>
struct CompileTimeAOrB
: public _Base
{
int Get() const
{
return _Base::Get();
}
};
#define USE_A
//#define USE_B
#ifdef USE_A
typedef CompileTimeAOrB<A1> Foo;
#endif
#ifdef USE_B
typedef CompileTimeAOrB<B1> Foo;
#endif
EDIT: Since A and B cannot be changed, introduced A1, B1 ;)
#include <iostream>
struct A
{
int value;
A() : value(2) {}
};
struct B
{
int value;
B() : value(4) {}
};
template <typename T>
int GetValue(T t)
{
return t.value;
}
int main()
{
A a;
B b;
std::cout << GetValue(a) << std::endl;
std::cout << GetValue(b) << std::endl;
return 0;
}
In order for it to work, you'd need to have the same variable or function named declared in each class you wanted this to work with.
In code (just paste and copy)is there a way to avoid repetition/listing of template args(line marked in code):
#include <iostream>
using namespace std;
template<class T,class... V>
struct nullptr_
{
nullptr_(T& obj,V&... args)
{
nullptr_hlp(obj,args...);
}
template<class A>
static void nullptr_hlp(A& a);
{
a = nullptr;
}
template<class A,class... Vs>
static void nullptr_hlp(A& a,Vs&... args)
{
a = nullptr;
nullptr_hlp(args...);
}
};
class X : nullptr_<int*,double*,char*>//IS THERE A WAY TO HAVE JUST nullptr_?
{
int* a;
double* b;
char* c;
typedef nullptr_<decltype(a),decltype(b),decltype(c)> init_;
public:
X():init_(a,b,c)
{
}
};
int main()
{
X x;
return 0;
}
nullptr_<int*,double*,char*> becomes an injected class name within X, so you can refer to it without the argument list:
class X : nullptr_<int*,double*,char*>//can't do away with the list here, unless you want to typedef it
{
int* a;
double* b;
char* c;
//typedef nullptr_<decltype(a),decltype(b),decltype(c)> init_; //don't really need this
public:
X():nullptr_(a,b,c) //can be used without the argument list
{
}
};
How about moving the typedef out of the class in to an anonymous namespace and use that for inheritance?
Is there a straightforward way for defining a partial specialization of a C++ template class given a numerical constant for one of the template parameters? I'm trying to create special constructors for only certain kinds of template combinations:
template <typename A, size_t B> class Example
{
public:
Example() { };
A value[B];
};
template <typename A, 2> class Example
{
public:
Example(b1, b2) { value[0] = b1; value[1] = b2; };
};
This example won't compile, returning an error Expected identifier before numeric constant in the second definition.
I've had a look through a number of examples here and elsewhere, but most seem to revolve around specializing with a type and not with a constant.
Edit:
Looking for a way to write a conditionally used constructor, something functionally like this:
template <typename A, size_t B> class Example
{
public:
// Default constructor
Example() { };
// Specialized constructor for two values
Example<A,2>(A b1, A b2) { value[0] = b1; value[1] = b2; };
A foo() {
A r;
for (size_t i = 0; i < b; ++b)
r += value[i];
return r;
}
// Hypothetical specialized implementation
A foo<A, 2>() {
return value[0] + value[1];
}
A value[B];
};
You need to put the specialization in the correct place:
template <typename A> class Example<A,2>
If you want to create a subclass:
template <typename A> class ExampleSpecialization : public Example<A,2>
The behavior for specializing on typedefs is similar to the behavior for specializing on an integer parameter.
I think this might work:
#include <iostream>
template <typename A, size_t B>
class Example {
public:
Example()
{
Construct<B>(identity<A, B>());
}
A foo()
{
return foo<B>(identity<A, B>());
}
private:
template <typename A, size_t B>
struct identity {};
template <size_t B>
void Construct(identity<A, B> id)
{
for (size_t i = 0; i < B; ++i)
{
value[i] = 0;
}
std::cout << "default constructor\n";
}
template <size_t B>
void Construct(identity<A, 2> id)
{
value[0] = 0;
value[1] = 0;
std::cout << "special constructor\n";
}
template <size_t B>
A foo(identity<A, B> id)
{
A r = 0;
for (size_t i = 0; i < B; ++i)
{
r += value[i];
}
std::cout << "default foo\n";
return r;
}
template <size_t B>
A foo(identity<A, 2> id)
{
std::cout << "special foo\n";
return value[0] + value[1];
}
A value[B];
};
int main()
{
Example<int, 2> example; // change the 2 to see the difference
int n = example.foo();
std::cin.get();
return 0;
}
Sorry, I just copy and pasted it from my test project. It's not really "specialization" in a way, it just calls overloads to specialized functions. I'm not sure if this is what you want and imo this isn't very elegant.
If memory serves, it should be more like:
template <typename A, size_t B> class Example
{
public:
Example() { };
A value[B];
};
template <typename A> class Example<A, 2>
{
public:
Example(A b1, A b2) { value[0] = b1; value[1] = b2; };
};
I don't think this is quite allowable as-is though -- there's nothing defining the types of b1 and/or b2 in the specialized version.
Edit [based on edited question]: Yes, a template specialization produces a new type that's not really related to the base from which it's specialized. In particular, the two do not share any of the implementation. You can't (by specializing a class template) produce a single type that uses one of two different ctors, depending on the value of a non-type parameter.
You can try something like this:
template<size_t s>
struct SizeTToType { static const size_t value = s; };
template<bool> struct StaticAssertStruct;
template<> struct StaticAssertStruct<true> {};
#define STATIC_ASSERT(val, msg) { StaticAssertStruct<((val) != 0)> ERROR_##msg; (void)ERROR_##msg;}
template <typename A, size_t B>
class Example
{
public:
Example() { };
Example(A b1){ value[0] = b1; }
Example(A b1, A b2) {
STATIC_ASSERT(B >= 2, B_must_me_ge_2);
value[0] = b1; value[1] = b2;
}
A foo() { return in_foo(SizeTToType<B>()); }
protected:
template<size_t C>
A in_foo(SizeTToType<C>) {
cout << "univ" << endl;
A r;
for (size_t i = 0; i < B; ++i)
r += value[i];
return r;
}
A in_foo(SizeTToType<2>){
cout << "spec" << endl;
return value[0] + value[1];
}
A value[B];
};
Working example on http://www.ideone.com/wDcL7
In templates if you are not using method it won't exists in compiled code, so this solution shouldn't make executable bigger because of ctors you can't use with some specialized class (for example Example<int, 1> should not have Example(A b1, A b2) ctor).
If you're goal is to only have to override a few methods/constructors in your specializations then maybe consider a generic base class to hold the common implementation for all Example templates so you don't have to rewrite it in every specialization you come up with.
For example:
template < typename A, size_t B >
class ExampleGeneric {
public:
// generic implementation of foo inherited by all Example<A,B> classes
void foo() {
A r;
for (size_t i = 0; i < B; ++i)
r += value[i];
return r;
}
// generic implementation of bar inherited by all Example<A,B> classes
void bar() {
A r;
for (size_t i = 0; i < B; ++i)
r *= value[i];
return r;
}
A values[B];
};
template < typename A, size_t B >
class Example : public ExampleGeneric<A,B> {
public:
//default to generic implementation in the general case by not overriding anything
};
//*** specialization for 2
template < typename A >
class Example<A,2> : public ExampleGeneric<A,2>{
public:
// has to be provided if you still want default construction
Example() {
}
//extra constructor for 2 parameters
Example( A a1, A a2 ) {
values[0] = a1;
values[1] = a2;
}
// specialization of foo
void foo() {
return values[0] + values[1];
}
// don't override bar to keep generic version
};
#include <iostream>
using namespace std;
template<typename _T, size_t S>
class myclass {
_T elem[S];
public:
myclass() {
for (int i = 0; i < S; i++) {
elem[i] = i;
}
}
void Print() {
for (int i = 0; i < S; i++) {
cout << "elem[" << i << "] = " << elem[i] << endl;
}
}
};
int main(int argc, char **argv)
{
myclass < int, 10 > nums;
nums.Print();
myclass < int, 22 > nums1;
nums1.Print();
}
That worked on my linux machine with
g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.