0

I'm trying to write some math code for a project and I stumbled upon a interesting problem:

template<class T> class Radian;
template<class T> class Degree;

template <class T>
class Radian
{
public:
    T mRad;
public:
    Radian(const Degree& d);
};

template <class T>
class Degree
{
public:
    T mDeg;
public:
    Degree(const Radian& d);
};

inline Radian::Radian ( const Degree& d ) 
{

}

inline Degree::Radian ( const Radian& d )
{

}

Note that the code is bigger but I removed unnecessary functions so you can understand it better.

I understand how templates work, I know they are generated at compile time but for this particular example I don't really understand what syntax I should use. I'm aware of the fact that in the constructors Degree should have a template argument but I'm not sure how to write it. From what I see I might have to use another template but it would be nice to use the same T for both since I won't convert from Degree_float to Radian_double.

Could anyone shed some light on this?

Thank you.

Sanctus2099
  • 1,669
  • 5
  • 22
  • 40

1 Answers1

5

This is the syntax for declaration:

template <typename T>
class Radian
{
public:
    Radian(Degree<T> const& degree);
    …
};

And this is the syntax for definition:

template <typename T>
inline Radian<T>::Radian(Degree<T> const& degree) {
    …
}

That is, since your class is a template, you need to specify its type argument. The constructor is not a template so you write it as you would a normal constructor, just prefixed with its class name.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • And make sure that you have placed all templated methods into a header file. – boto Jul 16 '12 at 15:37
  • ..and maybe it should be mentioned that he/she uses a wrong constructor name in `inline Degree::Radian ( const Radian& d )` – Desmond Hume Jul 16 '12 at 15:38
  • And take off the inline keyword because templates are automatically inlined. – blockchaindev Jul 16 '12 at 15:39
  • @Fixxxer *Are* class members automatically inlined? Free function templates at least aren’t. – Konrad Rudolph Jul 16 '12 at 15:39
  • @Fixxxer See the beginning of the accepted answer for my bounty question: http://stackoverflow.com/questions/11420802/code-organization-across-files-that-has-to-deal-with-template-functions-and-inli – Desmond Hume Jul 16 '12 at 15:42
  • @Desmond Not sure I trust that answer though since it neglects to address the fact that `inline` affects linkage rules (and there it’s definitely *not* just a hint to the compiler), and class templates automatically have internal linkage. So maybe out-of-line declared members of class templates inherit the linkage from their class? – Konrad Rudolph Jul 16 '12 at 15:45
  • @KonradRudolph Well, as long as there is separation between compiler and linker, `inline` _is_ just a hint to the compiler and _doesn't_ affect linkage when used with templates (which is the context of the question) because templates are automatically exempted from ODR. – Desmond Hume Jul 16 '12 at 15:57
  • @Desmond *Class* templates. Not function templates. To be honest, I don’t know what the C++ standard has to say here but I *do* know that this is a problem on some not too old version of g++ which required us to explicitly declare all our function templates as `inline` in a rather big library. – Konrad Rudolph Jul 16 '12 at 16:42