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 4555294
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:12:19+00:00 2026-05-21T17:12:19+00:00

I’m building a datastructure class with an std-like interface, and implementing different iterators for

  • 0

I’m building a datastructure class with an std-like interface, and implementing different iterators for the data structure.

Conceptually, what I would like to do is something like this:

template <class DataT>
class DataStructure
{
protected:
    DataT& Data;
public:
    DataStructure(DataT& data) : Data(data) {}
    class BaseIterator
    {
    public:
        BaseIterator()
        {
            cout<<"BaseIterator"<<endl;
        }
    };

    class DerrivedIterator1 : public BaseIterator
    {
    public:
        DerrivedIterator1()
        {
            cout<<"DerrivedIterator1"<<endl;
        }
    };

    class DerrivedIterator2 : public BaseIterator
    {
    public:
        DerrivedIterator2()
        {
            cout<<"DerrivedIterator2"<<endl;
        }
    };

    template<class IterT>
    IterT Begin()
    {
        //none-specialized implementation. Possibly throw exception
    }

    template<>
    DerrivedIterator1 Begin<DerrivedIterator1>()
    {
        //Find beginning for DerrivedIterator1
    }

    template<>
    DerrivedIterator2 Begin<DerrivedIterator2>()
    {
        //Find beginning for DerrivedIterator1
    }
};

But this of course does not compile since C++ doesn’t allow to specialize template member functions in none-specialized template containers.

The obvious workaround is of course to declare 2 different functions: Begin_Iterator1 and Begin_Iterator2 and be done with it. But I’m looking for a workaround that doesn’t change the interface.

Any ideas?

Edit: I forgot to mention that this is for a HW assignment and so boost and even std is not an option.

  • 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-21T17:12:20+00:00Added an answer on May 21, 2026 at 5:12 pm

    Function templates cannot be specialized in C++, point.

    It does not matter whether they are members of template or not, specialization of function templates is not allowed. Normally when using argument types to infer the template arguments, overloading does the same specialization would, so specialization for functions (and the associated extra complexity in overload resolution and such) was not deemed necessary.

    You however don’t have any arguments to infer on and would instantiate the templates manually. No,

    DataStructure::DerivedIterator1 i = dataStructure.Begin();
    

    would not work as you wrote the code, because type inference, just like overload resolution is only done on the arguments, not expected return value. You’d have to write:

    DataStructure::DerivedIterator1 i = dataStructure.Begin<DataStructure::DerivedIterator1>();
    

    and that has zero benefit over:

    DataStructure::DerivedIterator1 i = dataStructure.BeginIterator1();
    

    However, the first expression can be made to work with some wizardry. First you have to define BeginIterator1 and BeginIterator2 and than you’d do a temporary to late-decide which one to construct:

    class DataStructure {
        ...
        class BeginIteratorConstructor {
            DataStructure &dataStructure;
        public:
            BeginIteratorConstructor(DataStructure &ds) : dataStructure(ds) {}
            operator DerivedIterator1() { return dataStructure.BeginIterator1(); }
            operator DerivedIterator2() { return dataStructure.BeginIterator2(); }
        };
        BeginIteratorConstructor Begin() { return BeginIteratorConstructor(*this); }
        ...
    };
    

    Now dataStructure.Begin() will return a temporary something, that will call BeginIterator1 if you cast it to DerivedIterator1 or call BeginIterator2 when you cast it to DerivedIterator2. If you pass it to something where the compiler can’t decide which one to cast to, it will die either because of ambiguous overload or because BeginIteratorConstructor is not in fact iterator and you’ll have to cast it explicitly.

    (You should carefully make as much of the BeginIteratorConstructor private, but I am not sure how far will the compiler allow you to go, so you’d have to experiment a bit)

    • 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
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into

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.