Template Class providing Key Type.

Started by
6 comments, last by snk_kid 18 years, 10 months ago
I want to use an interface class to describe the method of accessing an object's key value, and comparing two key values. Is there any way that I can typedef the interface's key type inside the accessor class template so that I can use it within accessor class functions?

template <class C, class Object>
class Foo {
  public:
    typedef C::Key T;

    T getValue() { return m_value; }
    void setValue(T value) { m_value = value; }

  private:
    T m_value;
};

// interface
class Bar {
  public:
    typedef int Key;
    bool compare(Key a, Key b) { return a == b; }
};

class Mub {
  public:
    int value;
};

Foo<Bar, Mub> zeep;

Class Foo fails to compile because of the typedef line. Is there any way that I can define T as the type Bar::Key within the Foo scope, so that I can use it in Foo member functions like those above? Thanks for all assistance. Simon Hill
Advertisement
Quote:Original post by Krylloan
Class Foo fails to compile because of the typedef line.


Yes because "Key" is dependant on the template type parameter C, you must use typename to say your referring to a nested type and not a static member, i.e.

typedef typename C::Key key_type;


Also your passing by value in your accessor methods are you sure you want that, and "get" methods should be constant member functions [wink].
typedef typename C::Key
should be what you are looking for.
(off-topic)
It is strange to see so much use of typename these days. It looks like an indication to the (over?)use of templates, which in turn should be an indication of our average level.
(/off-topic)
Try this, it might work. Note that ir probably won't though, as I'm no template wizard.. Just another guy trying to pummle them into some semblance of working order [grin]
typedef typename C::Key T;

[EDIT] Wow. Three posts while I typed this minor reply..

Emmanuel, is that a good or bad? [smile]
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Quote:Original post by SirLuthor
Emmanuel, is that a good or bad? [smile]


[grin] looking at just one of my files of src code, i have 20 occurrences of the keyword typename (thats excluding template < typename ... >), here is a snippet of it:

template < typename VisitableTypes, typename Seq >struct remove_visited: mpl::reverse_copy_if<	VisitableTypes,	boost::is_same<		mpl::find<Seq, _1>,		typename mpl::end<Seq>::type	>> {};template < typename Visitor, typename Seq >struct visitor_impl : mpl::inherit_linearly<	typename remove_visited<		typename Visitor::visitable_types,		Seq	>::type,	visitor_implement<		_2,		typename boost::mpl::back<                    typename Visitor::visitable_types                >::type,		_1        >,	Visitor>::type {};
Looks like some crazy stuff... Just out of curiosity, what's it for?
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Quote:Original post by SirLuthor
Just out of curiosity, what's it for?


Its part of some code thats like loki's generic cyclic visitor written in terms of boost MPL, added some constant-correctness and extra bits. That bit of code partially involved in automatic generation of implementation if you do not want to have to implement visitation for a certain set of types.

[Edited by - snk_kid on June 23, 2005 3:16:51 PM]

This topic is closed to new replies.

Advertisement