Templated classes and argument/return type

I am starting to experiment with templates, having accepted the messy inconvenience of having to both declare AND defining the functions within the same header file.

One problem I have encountered is how to declare and define a member function that returns or accepts as an argument an object of its own type. What I mean is, by example of a non-template class:

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
class CArray{
public:
    //other contructors.
    CArray(const CArray &cArray);
    const CArray& operator=(const CArray &cArray);
    //other member functions.
    private:
    const CArray& Deep_Copy(const CArray &cArray);

   int *m_piArray=nullptr;
};

CArray::CArray(const CArray &cArray)
{
    Deep_Copy(cArray);
}

const CArray& CArray::Operator=(const CArray &cArray)
{
    Deep_Copy(cArray);

    return *this;
}

const CArray& CArray::Deep_Copy(const CArray cArray)
{
    //code to perform deep copy;
}


So far, so good - I can understand the code. However, to do the same thing in a templated class would, I THINK be declared like this:

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
template<typename dataType>
class tCArray{
public:
    //other contructors.
    tCArray(const tCArray &tcArray);
    const CArray& operator=(const tCArray &cArray);
    //other member functions.
    private:
    const tCArray& Deep_Copy(const tCArray &cArray);

    dataType *m_pTArray=nullptr;
};

template<typename dataType>
tCArray<typename dataType>::tCArray(const tCArray<typename dataType> &cArray)
{
    Deep_Copy(cArray);
}

template<typename dataType>
const tCArray<typename dataType>& tCArray<typename dataType>::Operator=(const tCArray<typename dataType> &cArray)
{
    Deep_Copy(cArray);

    return *this;
}

template<typename dataType>
void tCArray<typename dataType>::Deep_Copy(const tCArray<typename dataType> &cArray)
{
    //code to perform deep copy;
}


Specifically the difference being - aside from prefixing every member function definition with 'template<typename dataType>' - that every time 'tCArray' is used to declare an argument or return-type it must be given as 'tCArray<typename dataType>'.

Have I got the right end of the stick? Because that feels incredibly cumbersome, but if I try to compile without doing so I get errors.
Last edited on
1
2
3
template<typename dataType>
const tCArray<dataType>& tCArray<dataType>::operator=(const tCArray &cArray)
{
would be enough
Actually tCArray<typename dataType> would not compile, as it expect a qualified name.

This might be interesting: http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords
Last edited on
MANY thanks MiiniPaa! I am working from the 'C++ The Complete Reference, 4th Edition' and sadly Schildt doesn't give any examples of returning templates object types or using them as arguments!

Could you explain to me why you need the <dataType> for the return value but not the argument?

I have learned to avoid StackOverflow - I did post there a bit at one time but it is such a poisonous, unfriendly community that I packed in and came here!
Schildt's books are notoriously bad.
http://www.seebs.net/c/c_tcn4e.html
http://www.cs.technion.ac.il/users/yechiel/CS/BadBooksC+C++.html

I suggest C++ Templates - The Complete Guide it is a little old and do not include C++11 features, but it should get you started.
Actually: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list

I have learned to avoid StackOverflow - I did post there a bit at one time but it is such a poisonous, unfriendly community that I packed in and came here!
Well, it is true, but it is also have a great knowledge base. Many threads here ends with stackoverflow link to the solution or explanation.

Could you explain to me why you need the <dataType> for the return value but not the argument?
Because compiler parses code left-to-right and when you get to the argument list (but not before) it knows that it is a tCArray<dataType> and all references to tCArray (without template parameters) are to threat as current class type. You can do this if you do not want to write template arguments:
1
2
template<typename dataType>
auto tCArray<dataType>::operator=(const tCArray &cArray) -> const tCArray&
Last edited on
VERY cool!!! Would you believe I just read about that 'auto'/'->' operator thing in the 'new to c++11' list the other day! It didn't make much sense to me at the time but now I think I can see what it means!

I suppose in regards stackoverflow one can always just hit them up for information and not join in with all the politics and confrontational bullshit!

You are not a fan of Herb Schildt then? I must admit I quite like his books - but I will certainly check out that title you suggest as well.
Usually late return type declaration is used with templated functions for return types dependent on arguments:
1
2
3
4
5
template<typename T, typename U>
auto add(T lhs, U rhs) -> decltype(lhs + rhs)
{
    return lhs + rhs;
}
http://www.cprogramming.com/c++11/c++11-auto-decltype-return-value-after-function.html
http://en.cppreference.com/w/cpp/language/decltype
I see - so by using 'auto' and '->' you are actually making the function even MORE general - by not setting out its return type until AFTER it has been given its arguments?

Does this tie in with 'Runtime Type Information'? That is another language feature I have never really understood.

Also, instead of Schildt is there an author you yourself would recommend? I'll say in advance I don't like that 'Ivor Horton' bloke. There was always something about how he wrote that got my back up!!!
Does this tie in with 'Runtime Type Information'?
Nope. Everything related to templated parts is calculated in compile time. RTTI is a mostly transparent part of the language which you probably will not directly encounter for quite some time.

Also, instead of Schildt is there an author you yourself would recommend?
Depending on which kind of books you want. Good beginner level are
C++ Primer (Do not mix up with C++ Primer Plus which is bad) by Stanley Lippman and Barbara Moo.
Accelerated C++ by Andrew Koenig and Barbara Moo

I posted link before to the large list of good C++ books. AFAIR all books I recommended are there too.
Topic archived. No new replies allowed.