Compile Error using template type in return statement

I am trying to use a template inside of a class the following way
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
#ifndef BIBTREEHANDLE_H
#define BIBTREEHANDLE_H

#include <typeinfo>
#include <iostream>

#include "Hit.h"
#include "UpdateHit.h"

class BIBTreeHandle
{
   public:
    BIBTreeHandle();
    virtual ~BIBTreeHandle();

    template<typename T>
      T * CurrentBIBHit();

  protected:

    Hit * fBIBHit; ///< pointer to the break in board hit
    UpdateHit * fBIBUpdateHit; ///< pointer to the break in board hit

};

template<typename T>
T * BIBTreeHandle::CurrentBIBHit()
{
  if(typeid(T) == typeid(Hit))
    return fBIBHit;

  if(typeid(T) == typeid(UpdateHit))
    return fBIBUpdateHit;

  return 0;
}

#endif // BIBTREEHANDLE_H 


and I am accessing the following way

1
2
Hit * hit = IO->GetBIBTree().CurrentHit<Hit>();
UpdateHit * updateHit = IO->GetBIBTree().CurrentHit<UpdateHit>();


where IO->GetBIBTree() returns BIBTreeHandle& type.

I used to have a separate function for Hit and UpdateHit, but want to have a template function to make other code easier.

The error I am receiving during compile time is the following

BIBTreeHandle.h: In member function 'T* BIBTreeHandle::CurrentBIBHit() [with T = Hit]':
AnalysisFunctions.cpp:125:   instantiated from here
BIBTreeHandle.h:33: error: cannot convert 'UpdateHit*' to 'Hit*' in return
BIBTreeHandle.h: In member function 'T* BIBTreeHandle::CurrentBIBHit() [with T = SBUpdateHit]':
AnalysisFunctions.cpp:200:   instantiated from here
BIBTreeHandle.h:30: error: cannot convert 'Hit*' to 'UpdateHit*' in return



Is there a way I can do this or do I have to use two functions?

(Not all code is present. The entire code is about 20 files, but enough is given to explain error)
You are using two functions. You may specialize the template.
¿Do you realize why is giving you error?

By the way, you are breaking encapsulation.
Thanks for the quick reply.

I am trying to use one template function instead of two functions inside of BIBTreeHandle. One to return fBIBHit and another to return fBIBUpdateHit. I think the error is coming from the compiler trying to compile the whole function with each specific type T that is passed, instead of just the section that is specific to that type.

How am I breaking encapsulation?
Yep. What I'm trying to say is that the compiler will generate two functions.
If you really want for the name to be the same
1
2
3
4
5
6
7
8
9
10
template<>
Hit *BIBTreeHandle::CurrentBIBHit<Hit>()
{
   return fBIBHit;
}
template<>
UpdateHit *BIBTreeHandle::CurrentBIBHit<UpdateHit>()
{
   return fBIBUpdateHit;
}



How am I breaking encapsulation?
You are too eager to give your internals.
And violating Demeter's law.
closed account (o1vk4iN6)
Why use a template function when you can define 2 functions returning the respective type making it less ambiguous ?
I have other functions that are templates and would like this function to be a template as well for consistency and simplicity. I could write a function for each. But it makes the code longer and just another function for the general user to have to remember, when they are writing their analysis code.

Can you please explain to me exactly how I am violating Demeter's Law? Is it because I have the variables under protected and not private?
Can you please explain to me exactly how I am violating Demeter's Law? Is it because I have the variables under protected and not private?


It's because you have "protected" variables that aren't really protected. The user can get at them and modify them at will with no restrictions.

As for these template functions, I don't think they should be templates. If you are trying to do stuff based on the type of something that was passed, you are probably doing it wrong.
closed account (o1vk4iN6)
I have other functions that are templates and would like this function to be a template as well for consistency and simplicity.


If you have other template functions that are used the same way as accessors, I think you are misusing templates and should probably change them as well. It isn't very simple for the end user who has no idea what class to use in the template function.

If you have one class that is a template class does that mean you should make all your other classes templates as well? Even when they are not needed and have no use, just for consistency ? That's a bad mindset to have (I think personally). You would only make a class template for something like a container where you want the type to be changeable.
Last edited on
Topic archived. No new replies allowed.