1

See,I have this code:

template<typename T=int>struct color
{
    T red, green, blue;
    color(T r, T g, T b)
    :red(r), green(g), blue(b)
    {
    }

    #ifdef SDL_BACKEND
    template<typename R,typename S> R map(S surf)
    {
        return SDL_MapRGB(surf->format,red,green,blue);
    }
    #endif /* SDL_BACKEND */
};

and I use it here:

pro::color<int> black(0,0,0);
SDL_FillRect(screen, 0, black.map(screen));

Now here's the error I'm getting:

error: no matching function for call to 'pro::color::map(SDL_Surface*&)'|

I'm not that experienced in templates, so I haven't seen this error before. What exactly is the problem?

NOTE: I didn't tag this with the "SDL" tag, because IMHO this question is more related to templates , the fact that I am using SDL is irrelevant. Also I'm using gcc-4.4x with -std=c++0x.

1 Answer 1

4

This has nothing to do with meta programming. It's just a matter of using templates correctly. The return type cannot be deduced, so you have to specify it; either in the function or in the template instantiation. I.e., pick one of these two:

// Version #1: Change function definition
template<typename S> Uint32 map(S surf) { return SDL_MapRGB(surf->format,red,green,blue); }


// Version #2: Change invocation
black.map<Uint32>(screen);

(In fact, I don't really understand why you need a template here at all. Why not just make the function into Uint32 map(SDL_Surface *)?)

Sign up to request clarification or add additional context in comments.

8 Comments

In fact, I don't really understand why you need a template here at all . I'm not doing something important, I just wrote this function to learn more about templates. +1 , by the way.
@IntermediateHacker: Rule 1 of Template Club: Don't use templates :-) (That is, "use them if you need them, not because you're there". Think of templates as you think of fire extinguishers.)
lol, that saves me a lot of extra trouble learning about them. :D
Agreed regarding the lack of necessity for templating. Documentation does indeed suggest that, since you are returning the result of SDL_MapRGB and SDL_MapRGB returns a Uint32, there's no point in making the return type be a template function. I'm not familiar with SDL, but it seems since your method only uses part of the SDL_Surface interface, you don't need the interface of any of its derived types and could merely take an SDL_Surface*&.
@IntermediateHacker: Well, you need them when you need them... as a very rough rule of thumb, you need class templates for generic data structures, and function templates when you need to deduce arguments. Those two often go hand in hand. A template in the right place is pure magic, but it comes at a considerable ontological cost.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.