How Can I Check In Templates , If T==Boolean ?

Pages: 12
Hi guys,

I need to add two parameters . I'm dealing with string/int/double and boolean.
The first three are doing great , meaning the ADD operation between them results in an exact output.However ,when T=bool , I get the operation of a LOGIC OR , and not a LOGIC AND .

The code :
I have 3 classes :
Management
LinkedList
Matrix


1
2
3
4
5
6
7
8
9
10
template <class T>
class Matrix
{

private:

	LinkedList<T> *m_list;

// more fields  ,irrelevant for the moment //
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
template <class T>
class LinkedList {

private:

	class Node		// each node represents a column
	{
	public:
		int _row;				// rownumber
		int _col;				// column number
		T _value;	 			// Some T 
		Node *_next;   	                        // pointer to next Node

                // initializations of Node

	};

	Node * _head; 		      // the head of the matrix

};

template <class T>
LinkedList<T>* LinkedList<T>::combineLists(LinkedList<T> *inputList)
{
	Node* inThis= this->m_head;
	Node* inInput= inInput->m_head;

...
... 
                 // same column and same row // 
  if (inThis->_row == inInput->_row && inThis->_col == inInput->_col) 
  {
   Node * newNode = new Node(inThis->_row , inThis->_col , inThis->_value+inInput->_value); // problematic line
   newList->attachNodeToLinkedList(newNode);
  }

...
}


As I said above,the program must support 4 kinds of inputs : char,int,double and bool.The first 3 are
working great ,you can see that the operation being done is "inThis->_value+inInput->_value" in
the IF condition above.

In my code ,when I get
0 1 -----> 1
1 0 -----> 1
0 0 ------> 0
1 1 ------> 1

but I need logical AND
1 0 = 0
0 1 = 0
0 0 = 0
1 1 = 1

I'd appreciate any help,
Thanks ,Ronen
Last edited on
Without giving any thought to what you are trying to accomplish, I can tell you that you can accomplish it using a template specialization for bool. Check this site's tutorial here: http://www.cplusplus.com/doc/tutorial/templates/.
webJose wrote:
I can tell you that you can accomplish it using a template specialization for bool

+1 ( Although what I use here techniquely is overloading, template specialization is the way
to go in the general case. Food for thought -> http://www.gotw.ca/publications/mill17.htm )

I would probably do something like this:

1
2
3
4
5
6
7
8
9
10
template <class T>
T AddOp(T left, T right)
{
    return left + right;
}

bool AddOp(bool left, bool right)
{
    return left && right;
}

And then:

1
2
3
4
5
if (inThis->_row == inInput->_row && inThis->_col == inInput->_col)
{
    Node * newNode = new Node(inThis->_row , inThis->_col, AddOp(inThis->_value,inInput->_value));
    newList->attachNodeToLinkedList(newNode);
}

Last edited on
Really, ¿why do you associate + with 'and'? If you work with positive logic (as your example shows) you are asking 1+0==0
See this thread http://www.cplusplus.com/forum/general/43248/#msg233885 (and next)

If you really want that 'combine' means 'and' use a template especialization
1
2
3
4
5
template <> //now replace every T with bool
LinkedList<bool>* //why are you returning a pointer, instead of an object?
 LinkedList<bool>::combineLists(LinkedList<bool> *inputList){ 
  //this is the function to be executed if T==bool
}
Last edited on
Okay ,but can I overload operator + within that template specialization ?
What I need is basically simple , when I have "bool+bool" , I want to do "bool && bool" .
Is this possible ?
thanks a lot!
Don't ask. Test! :-S What is the gain in waiting for a "Yes, you can do it.", or "No, it cannot be done." Test and find out yourself. Don't have a compiler around? I recently learned there are online compilers. I like ideone.com.
Ronen wrote:
can I overload operator + within that template specialization ?

No. Not unless you create a wrapper type around bool. But it's simpler to just define a function.

ne555 wrote:
¿why do you associate + with 'and'?

Well, this is a good question. Usually '+' means 'or' and '*' means 'and'. If you decide to
use this interpretation, you don't have to do anything. (check the thread ne555 suggests)
Last edited on

m4ster r0shi , I've tested both the overloading (it doesn't work , i've checked for the last few hours) and your example of :

template <class T>
bool AddOp(bool left, bool right)
{
return left && right;
}
template <class T>
T AddOp(T left, T right)
{
return left + right;
}

and when T=bool , it goes to "T AddOp(T left, T right)" and not to "bool AddOp(bool left, bool right)". thanks
Remove the template<class T> from the bool version


1
2
3
4
5
6
7
8
9
10
11
12
//specialised or non template version for bool
bool AddOp(bool left, bool right)
{
return left && right;
}


template <class T>
T AddOp(T left, T right)
{
return left + right;
}
Hi,
I've already tried that , here is the error :

C:\Users\T\workspace\CPP05\Debug/../LinkedList.h:90: multiple definition of `addOp(bool, bool)'
main.o:C:\Users\T\workspace\CPP05\Debug/../LinkedList.h:90: first defined here

I don't under what might be the problem ,maybe something to do with consts ?
What if you try some real specialization?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template <class T>
struct AddOp
{
    T operator()(T left, T right) { return left + right; }
};

template <>
struct AddOp<bool>
{
    bool operator()(bool left, bool right) { return left && right; }
};

//...

if (inThis->_row == inInput->_row && inThis->_col == inInput->_col)
{
    Node * newNode = new Node(inThis->_row , inThis->_col, AddOp<T>()(inThis->_value,inInput->_value));
    newList->attachNodeToLinkedList(newNode);
}
Last edited on
He can always post the whole code
m4ster r0shi ,again , my sincere gratitude .
Second, where do I need to put this ?

within the LinkedList.h or outside it ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
template <class T>
class LinkedList {

private:

	class Node		// each node represents a column
	{
	public:
		int _row;				// rownumber
		int _col;				// column number
		T _value;	 			// Some T 
		Node *_next;   	                        // pointer to next Node

                // initializations of Node

	};

	Node * _head; 		      // the head of the matrix

};





// here ?? because when I put it here ,I get errors again 
template <class T>
class AddOp
{
    T operator()(T left, T right) { return left + right; }
};

template <>
class AddOp<bool>
{
    bool operator()(bool left, bool right) { return left && right; }
};






template <class T>
LinkedList<T>* LinkedList<T>::combineLists(LinkedList<T> *inputList)
{
	Node* inThis= this->m_head;
	Node* inInput= inInput->m_head;

...
... 
                 // same column and same row // 
  Node * newNode = new Node(inThis->_row , inThis->_col, AddOp<T>()(inThis->_value,inInput->_value));
    newList->attachNodeToLinkedList(newNode);
...
}




\LinkedList.h:110: error: expected primary-expression before 'template'
..\LinkedList.h:110: error: expected ';' before 'template'
..\main.cpp:22: error: expected '}' at end of input

thank you very much !!
Last edited on
Well, there are syntax errors in your code (missing semicolon(s)
and bracket(s)). I'm sure you can find them if you take a close look.
There aren't any , I've checked three times .
Can you post the whole code? If it's too big you can put it here -> http://pastebin.com/
I've sent you a PM.Please read it.
10x.Ron
Ok, I did. I sent one too.
Going into hiding is not fair.
Especially to those who also contributed to this thread.
I thought it so rude... but then thought what the hell.
Last edited on
Pages: 12