Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7947201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:21:55+00:00 2026-06-04T01:21:55+00:00

I was implenting a TypeList based on Loki, based on reading from: http://aszt.inf.elte.hu/~gsd/halado_cpp/ch06s09.html and

  • 0

I was implenting a TypeList based on Loki, based on reading from:

http://aszt.inf.elte.hu/~gsd/halado_cpp/ch06s09.html

and saw this code from the site for IndexOf (to find the index of a type in the typelist):

template <class T>
struct IndexOf< NullType, T>
{
    enum { value = -1 };
};

template <class T, class Tail>
struct IndexOf< Typelist<Head, Tail>, T>
{
private:
    enum { temp = IndexOf<Tail, T>::value };
public:
    enum { value = (temp == -1) ? -1 : 1+temp };
};

It doesn’t seem like that would work because nowhere do I see something comparing T as the list is recursively traversed. In my implementation it looks like this:

template<typename Tlist, typename T>
struct IndexOf
{
private:
  static const int temp = IndexOf<typename Tlist::Tail, T>::value;
public:
  static const int value = (temp == -1) ? -1 : 1 + temp;
};

template<typename T>
struct IndexOf<NullType, T>
{
  static const int value = -1;
};

and, in fact, always returns -1. If I think of it, imagine one has TypeList; then Tail will be NullType, so temp will be -1 by the specialization and then value will be -1..even if Head was char and I would have expected zero. What am I missing here?

Thanks


My implementation of Typelist is merely:

template<typename H, typename T>
struct Typelist
{
  typedef H Head;
  typedef T Tail;
};

I’m guessing this isn’t Lokis, but With Joel’s answer I got this working for me:

template<typename Head, typename Tail, typename T>
struct IndexOfImpl
{
private:
  static const int temp = IndexOfImpl<typename Tail::Head, typename Tail::Tail, T>::value;
public:
  static const int value = (temp == -1) ? -1 : temp + 1;
};

template<typename T, typename Tail>
struct IndexOfImpl<T, Tail, T>
{
  static const int value = 0;
};

template<typename T>
struct IndexOfImpl<T, NullType, T>
{
  static const int value = 0;
};

template<typename Head, typename T>
struct IndexOfImpl<Head, NullType, T>
{
  static const int value = -1;
};

template<typename Tlist, typename T>
struct IndexOf
{
 public:
  static const int value = IndexOfImpl<typename Tlist::Head, typename Tlist::Tail, T>::value;
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:21:56+00:00Added an answer on June 4, 2026 at 1:21 am

    Should be :

    template <class T>
    struct IndexOf< NullType, T>
    {
        enum { value = -1 };
    };
    
    template <class T, class Head, class Tail>
    struct IndexOf< Typelist<Head, Tail>, T>
    {
    private:
        enum { temp = IndexOf<Tail, T>::value };
    public:
        enum { value = (temp == -1) ? -1 : 1+temp };
    };
    
    template <class T, class Tail>
    struct IndexOf< Typelist<T, Tail>, T>
    {
    public:
        enum { value = 0 };
    };
    

    You try to find T at some Head in the recursive encoding of the list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While implementing XML file reading/writing in my application I saw that when I call
i'm having issues implenting a dynamic action-based menu system in a PartialView as below:
I noticed while reading through my code that I have a static member function
I'm having trouble implmenting the 3rd parameter in the function documented here: http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/reference/async_read_until/overload4.html What
I am trying to get from localNodes the Node who has the minimum distance
I have developed an asp.net control that inherits from the gridview and its called
When generating dynamic content, which way is better to simply create the HTML and
I implementing OLE DB provider for my custom database. It will be used from
When implementing SuperBoxSelect (http://www.sencha.com/forum/showthread.php?69307-3.x-Ext.ux.form.SuperBoxSelect), I've realized that it currently does not support shift +
Hi I'm having trouble implenting an answer to a previous question // on calling

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.