-7

Alright guys!

I'm having a small problem here, to make it simpler, this is more or less how it looks like:

window.h

template <ObjectType &x>
class foo { stuff }

game.h

class gameclass { ObjectType a; }

main.cpp

gameclass mygame;
foo<mygame.a> fooa;

Now the fooa fails as somehow it doesn't allow me to send mygame's member 'a' as a template argument.

Now how can I send a class member as a template parameter?

Thank You

Miguel P
  • 1,262
  • 6
  • 23
  • 48
  • 6
    It doesn't make sense to. The template argument is supposed to be a *type*, not an object. If you want the *type* of `a`, then just do `foo fooa;`. – Joseph Mansfield Apr 06 '13 at 21:34
  • Your `foo` template expects a *type*. You are passing a value computed at run-time. How do you expect to use `x` in the part marked as "stuff"? It's not clear what you want to do – Andy Prowl Apr 06 '13 at 21:34
  • 3
    You forgot to read your C++ book. – Lightness Races in Orbit Apr 06 '13 at 21:35
  • Sorry, wrote it wrong, please check now! And you don't need to vote down, that's just being rude... – Miguel P Apr 06 '13 at 21:46
  • I think @LightnessRacesinOrbit would not change his mind after this last edit (quite understandably, I must say). You should really read a book which covers templates. – Andy Prowl Apr 06 '13 at 21:50
  • 4
    @MiguelP: Downvotes are not a way to be rude (or at least, they should not be used as such). If you will hover the mouse on the downvote button, you should see a tooltip appearing that lists 3 possible motivations for downvoting: no research effort (any book on templates tells you what you are trying to do is non-sense), unclear (and your question really is, since the "stuff" part is not shown at all), or not useful (and I personally do not find this useful). So for me, all three criteria for downvoting are met. I downvoted, but I did not do so for being rude. – Andy Prowl Apr 06 '13 at 21:53
  • Still, you cannot pass a *pointer-to-member* as you've written. See my answer. I also agree with Andy Prowl, although I didn't downvoted you. – Synxis Apr 06 '13 at 21:55
  • @sftrabbit you are wrong: a template argument does not need to be a `type`. It can be an integer or a member-function pointer. The latter is what is wanted here, I think. – Walter Apr 06 '13 at 22:01
  • 1
    @Walter You are right, but sftrabbit wrote his comment with the first revision of question, which was specifying `` ;) – Synxis Apr 06 '13 at 22:02
  • @Synxis how can I know that ... sorry sftrabbit. – Walter Apr 06 '13 at 22:03
  • 2
    @Walter Haha, it's okay. I think comments (and answers) that were posted to old revisions should have a little revision reference next to them that you can click to see the differences. – Joseph Mansfield Apr 06 '13 at 22:04

1 Answers1

1

It seems you want to pass a non-type template parameter, and more specifically, a pointer-to-member. For what you want, you have to user:

template<ObjectType gameclass::* ptr>
struct foo
// ...

Note that will allow you to access the member of an object only if you have a pointer to that object.

You can have more details here (pointer-to-member as template parameter) or here (pointer-to-members).

Note that you cannot pass the value of mygame.a to a template in the code you posted, because:

  1. template parameters are compile-time, your value is runtime.
  2. non-type template parameters cannot be any object type, only integral (or enum), pointer/reference to object/function, or pointer-to-member.

EDIT: You have changed your template to template<ObjectType& x>. This is correct for reference to object. However, mygame.a is not a reference, it is a value. To specify the member, you have to use the syntax &mygame::a, which forces you to use a pointer-to-member.

Community
  • 1
  • 1
Synxis
  • 9,236
  • 2
  • 42
  • 64