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

  • Home
  • SEARCH
  • 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 6675417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:51:11+00:00 2026-05-26T03:51:11+00:00

I’m running into an issue while trying to write a recursive template member function

  • 0

I’m running into an issue while trying to write a recursive template member function for iterating through tuples.

In the following code:

#include <cstddef>
#include <iostream>
#include <string>
#include <tuple>

template <typename... P>
class A
{
public:
  typedef std::tuple<P...> tup_t;

  tup_t tup;
};

template <typename T, typename... P>
class AA : public A<P...>
{
public:
  T junk;
};

template <typename T>
class B
{
public:
  T a;

  void func(const char* delim);

private:
  template <size_t x>
  void __func(const char* delim);
};

template <typename T>
void B<T>::func(const char* delim)
{
  __func<std::tuple_size<typename T::tup_t>::value>(delim);
}

template <typename T>
template <size_t x>
typename std::enable_if<(x > 1), void>::type
B<T>::__func(const char* delim)
{
  std::cout << std::get<x-1>(a.tup) << delim;

  __func<x-1>(delim);
}

template <typename T>
template <size_t x>
typename std::enable_if<(x == 1), void>::type
B<T>::__func(const char* delim)
{
  std::cout << std::get<x-1>(a.tup) << std::endl;
}

int main()
{
  typedef A<int,float,std::string> T_first;
  B<T_first> b;

  std::get<0>(b.a.tup) = 5;
  std::get<1>(b.a.tup) = 4.0;
  std::get<2>(b.a.tup) = "string";

  b.func(" - ");

  typedef AA<int,std::string,double,size_t> T_second;
  B<T_second> bb;

  std::get<0>(bb.a.tup) = "test";
  std::get<1>(bb.a.tup) = 3.0;
  std::get<2>(bb.a.tup) = std::tuple_size<T_second::tup_t>::value;

  bb.func(" => ");

  return 0;
}

When I compile with:

$ g++-4.5 -std=c++0x -W -Wall -pedantic-errors test6.cpp

I get the following errors:

test6.cpp:60:1: error: prototype for ‘typename std::enable_if<(x > 1), void>::type B<T>::__func(const char*)’ does not match any in class ‘B<T>’
test6.cpp:31:32: error: candidate is: template<class T> template<unsigned int x> void B::__func(const char*)
test6.cpp:70:1: error: prototype for ‘typename std::enable_if<(x == 1), void>::type B<T>::__func(const char*)’ does not match any in class ‘B<T>’
test6.cpp:31:32: error: candidate is: template<class T> template<unsigned int x> void B::__func(const char*)

Now, if I instead define B<T>::__func inside the class like:

  template <size_t x>
  typename std::enable_if<(x > 1), void>::type
  __func(const char* delim)
  {
    std::cout << std::get<x-1>(a.tup) << delim;

    __func<x-1>(delim);
  }

  template <size_t x>
  typename std::enable_if<(x == 1), void>::type
  __func(const char* delim)
  {
    std::cout << std::get<x-1>(a.tup) << delim;
  }

It compiles fine.

I really don’t like implementing the functions within the class declaration, so I’d appreciate it if someone could point out where my original attempt went wrong.

Is it:

template <typename T>
template <size x>

Should it be written in a different manner?

Compiler Version: gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4)

Thanks,

P.S. Please don’t make fun of my simplified test case. The project that spawned this scenario is a little more impressive than this example… but only slightly.

  • 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-05-26T03:51:12+00:00Added an answer on May 26, 2026 at 3:51 am

    The types have to match in both the declaration and definition of __func. So in the class definition you must declare __func as:

    template <size_t x>
    typename std::enable_if<(x > 1)>::type
    __func(const char* delim);
    

    (This is equivalent to using std::enable_if<(x > 1), void> as the second template parameter defaults to void.)

    The reason for this restriction has to do with template specializations. And in fact since you’re using std::enable_if you’re relying on those specializations as std::enable_if<true, T> is a specialization. Note also the mismatch in the case of __func<0> which doesn’t have return type void. It ‘has’ return type std::enable_if<false, void>::type, which doesn’t exist, and hence is invalid (and is then removed via SFINAE).

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to loop through a bunch of documents I have to put
I am currently running into a problem where an element is coming back from
I have a French site that I want to parse, but am running into
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.