Click here to Skip to main content
15,880,364 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a class with a template parameter T.
the parameter is of type bool, and can be set to true or false.
lets say, I have a private member in the class, name "var".
if the parameter value is true I need "var" to be of type int,
and if the parameter value is false I need the type of "var" to be double.

How can I implement this "conditional type" of "var"?

additional information copied from non-solution below
template <bool value="">
class cls
{
private:
   ///define int var if value==true
   ///define double var if value==false
   ...
   ...
public:
   ...
};


how can I implement this conditional type of var, depending on the template type?
Posted
Updated 11-May-14 10:19am
v3
Comments
[no name] 11-May-14 8:40am    
It is not clear that you understand templates. T as a bool is absolutely useless and misses the point of templates completely. Post your code.
[no name] 11-May-14 11:16am    
So it is!
oO_OFIR_Oo 11-May-14 11:19am    
no, I dont need it to be exactly like this, I used this example to simplify it.
lets say I have a template-class, and I need based on the template type, if it is X, to have a Type1 var in the class, and if the template type is Y, I need to have a Y member in the calss.
[no name] 11-May-14 11:23am    
templates is "something very general". There is no "if type == ...". Types have to serve the "template"'s requirements.
Nelek 11-May-14 16:32pm    
I have edited my answer, after your comment. I think you should explain a little bit more about why do you want to declare thing into the template. In my opinion you are mixing concepts and trying to make the square a circle. Don't get me bad, it is not a blame.

I just think it would be better and easier for us to help you, if we know a bit about you background thoughts.

You can use type trait in your code.
Like this
C++
template <bool value>
struct type_extractor
{
    typedef int type;
};

template <>
struct type_extractor<false>
{
    typedef double type;
};

template <bool value>
class cls
{
private:
   typename type_extractor<value>::type _intOrDouble;
   ...
   ...
public:
   ...
};



type_extractor<value>::type will extract desired (mapped) type for you.
 
Share this answer
 
v4
Comments
Joren Heit 11-Jun-14 17:06pm    
+5, better solution than specializing the entire class! Was just about to post something similar myself, when I saw this.
What you're asking is definitely possible.

First you declare a template class like so -
C#
template <bool>
class cls
{
private:
    int var;

public:
    void Print() { std::cout << "var is an int\n"; }
};

You then specialize the template class like so -
C#
template <>
class cls<false>
{
private:
    double var;

public:
    void Print() { std::cout << "var is a double\n"; }
};

Now when you create an object with cls<true>, the first template class will be used.
When you create an object with cls<false>, the specialized template class will be used.
XML
int _tmain(int argc, _TCHAR* argv[])
{
    cls<true> a;
    a.Print();

    cls<false> b;
    b.Print();

    return 0;
}
 
Share this answer
 
hi,
great answers already provided. Still i would suggest taking a look at http://www.cprogramming.com/tutorial/template_specialization.html[^]
the code example is well explained there.
hope this helps!
 
Share this answer
 
I don't really understand what you try to do, but templates is something quite general. You can do cool things with them, but you are "limitated" with the programing freedom in them.

I wrote time ago an article expanding a very cool template of Simon Hughe.

The template is MFC based, but I think it can help you to have an idea and maybe understand some concepts.

The link is:An Addition to Smart List classes[^]

You really can compare types, but I would not recommend you to make a lot of conditional things in there. Templates should stay as simple as possible to be as flexible as needed

Hope it helps :)

Edit 01 (after deleted comment and non-solution update)
That is what I was saying before. I don't think that is a good idea.

The template can handle complexe TYPEs for you (if you read my article, you can have a look to the message board to see what I mean)

I don't see why it would be needed to declare different variables into the template. In my opinion, such kind of logic should be outside in your program.

You should create the template in a way that can handle everything, and depending on what you need then you pass one thing or another.

In my tesis I had 3 different set of objects, each one with different data structure and different parameters, variables and methods; but the template was only one and it worked with all of them.
 
Share this answer
 
v3
If you google you will find many tutorials on template classes in C++ such as this: http://www.learncpp.com/cpp-tutorial/143-template-classes/[^]

Please read some of these to get an understanding of the methodology of templates. To apply templates to your problem you would end up with something like this:

C++
#include <iostream>

using namespace std;

template <class T> class myclass
{
public:
	myclass(T initialiser) { myvariable = initialiser;} //  constructor

	void print() { cout << myvariable << endl;};

private:
	T myvariable;

};

int main()
{
	int itest = 2;
	double dtest = 3.54545;

	myclass<int> iclass(itest);
	myclass<double> dclass(dtest);

	iclass.print();
	dclass.print();
	
	system("pause > nul"); // To pause program 
}


Please remember I can only respond to your question and cannot be sure that templates are the best solution to your problem without knowing the problem.
 
Share this answer
 
v5
1. Short answer: you shouldn't! Template specializaton (not partial specialization btw - partial specialization implies at least two template parameters, which you haven't!) is definitely the wrong way to do whatever you meant to do: specialization of a class requires you to rewrite the entire definition of the code, so you have two separate class definitions, and you have nothing gained by using the template syntax.

2. If you have a reason to use a template at all, you should simply pass the desired value type as template argument rather than a boolean non-type argument:
C++
template <class value_type>
class cls {
private:
   value_type var;
   ...
public:
   ...
};

If you need a member function that behaves differently, simply use polymorhism:
C++
template <class value_type>
class cls {
private:
   value_type var;
   void print_type(int) {
      std::cout << "this is int" << std::endl;
   }
   void print_type(double) {
      std::cout << "this is double" << std::endl;
   }
public:
   void print_type() {
      print_type(var);
   }
   ...
};
 
Share this answer
 
v7

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900