> Are you redefining the original template? Or is this a new template function for megaptr?
> Because in main you only use the one template parameter <std::string> but two templates are defined?
>> I think I understand the second template function now after re-writing it, I believe it's a template constructor
>> ( I didn't think you could do that :) and it seems as if the template makes an argument parameter pack
>> which is forwarded the constructor arguments. please let me know if I was close.
Your understanding is spot on.
Parameter packs:
http://en.cppreference.com/w/cpp/language/parameter_pack
The construct
T&& for some deduced type
T is what Scott Meyers calls a 'universal reference'.
http://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers
Perfect forwarding:
http://thbecker.net/articles/rvalue_references/section_01.html
The whole article is worth careful perusal; sections 7 and 8 are about perfect forwarding.
>>> And another question, is this useless code?
There may be (are) much better (correct, robust) alternatives available in the standard library; in practice one would a
std::unique_ptr<> instead of a
megatron::utility::megaptr<>.
But it is, emphatically, not
useless code. While learning C++, any code (that we write ourselves) that teaches us something is the most useful kind of code we can write.
>>>> Maybe something that would test me further?
Consider: what would happen if a copy of a
megaptr<> object was made?
When both the copy and the original are destroyed?
Then read up on
std::unique_ptr<>
http://en.cppreference.com/w/cpp/memory/unique_ptr
http://www.devx.com/cplus/10MinuteSolution/39071