SFML community forums

Help => General => Topic started by: lol123 on December 09, 2013, 04:16:06 pm

Title: sf::Texture as a template argument
Post by: lol123 on December 09, 2013, 04:16:06 pm
I made my own Tuple class:

template <class T1, class T2>
class Tuple
{
public:
    Tuple (T1 Item1, T2 Item2) : item1(Item1), item2(Item2) { }
    T1 item1;
    T2 item2;
};

But when I use it:
Tuple<MyOwnClass, sf::Texture>

I get error:
template argument 2 is invalid
Title: Re: sf::Texture as a template argument
Post by: eXpl0it3r on December 09, 2013, 04:42:48 pm
Such an error can come from more than just the template (e.g. cyclic dependencies), could you provide a complete and minimal example with the template?

Secondly, is this class just for learning purposes or why aren't you using std::pair?

And finally, you should not be storing a full texture that way. Textures are heavy resources. If you simply assign an texture to your tuple it will make a copy. Copying a texture is really a bad idea, because it involves moving the data from the VRAM to the RAM and back to the VRAM.
At best you should wrap the texture in a smart pointer (unique_ptr or shared_ptr), bundle it to your class/type with a std::pair and put it into a STL container. ;)
Title: Re: sf::Texture as a template argument
Post by: lol123 on December 09, 2013, 05:33:21 pm
1) Just tried to reproduce this error, but everything worked fine! Guess I did something wrong that time
2) I was trying to benchmark my Tuple and std::pair performance

Thanks!
Title: Re: sf::Texture as a template argument
Post by: Nexus on December 09, 2013, 07:36:45 pm
I was trying to benchmark my Tuple and std::pair performance
You will lose, already because you copy the parameters unnecessarily, let alone move semantics or perfect forwarding. Don't reinvent the wheels of the standard library, they have lived through years of research, development and optimization. Even something seemingly simple like std::pair is non-trivial to implement safely and efficiently.