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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:02:06+00:00 2026-05-26T18:02:06+00:00

I have a C++ class Finder that stores a location in containers plus some

  • 0

I have a C++ class Finder that stores a location in containers plus some additional data. Such containers have the type std::string but also char * or MyString. Say, the class looks like this (Iterator<TContainer>::Type is a trait / metafunction that returns the iterator type for a given container):

template <typename TContainer>
class Finder
{
public:
  typedef typename Iterator<TContainer>::Type TIterator;
  TIterator iterator;

  // Initialization of iterator: How to do it generically?

  // Finder {}                       // problem with pointers!
  // Finder() : iterator(0) {}       // problem with iterators!
};

The main problem now is how to initialize the iterator that can be a pointer or an iterator. If I only wanted to support my own containers then I could simply implement a constructor that takes nullptr_t following the nullptr idiom. However, since I want to support pointers, my own iterators and STL iterators, I’m a bit out of depth here.

The best thing that I can imagine is to write a getNil<>() function, e.g. the code below. However, now at least three questions arise:

  1. Is this the best way to achieve my aim?

  2. How to determine whether a type is a STL iterator? I would probably need some #ifs, and tailor code to each compiler/STL version to use.

  3. Is it possible at all to get a nil iterator in the STL? Is the result of x-y in std::vector<int> x, y; int x = x-y; defined at all?

The code:

// Forward declaration.
template <typename T>
T getNil<T>();

template <typename TValue> TValue * getNil<TValue *>()
{ return NULL; }

template <typename TValue> TValue * const getNil<TValue * const>()
{ return NULL; }

template <> TValue * const getNil<MyIterator>()
{ return MyIterator(nullptr); }  // nullptr from above idiom

template <> TStlIterator
boost::enable_if<
    MagicallyDetermineIfIsStlIterator<TStlIterator>::value,
    TStlIterator>
getNil<TStlIterator>()
{ return MyIterator(nullptr); }  // nullptr from above idiom
  • 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-26T18:02:07+00:00Added an answer on May 26, 2026 at 6:02 pm
    Finder() : iterator() { }
    

    Should do the trick. Not providing an argument for a member in the initialization list will call the default constructor on a type that has one, and will zero-initialize POD types including pointers (and it will fail if the type has no default constructor, but that seems unlikely given your scenario).

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

Sidebar

Related Questions

I have class method that returns a list of employees that I can iterate
I have class with a member function that takes a default argument. struct Class
I have a derived class that's a very thin wrapper around a base class.
I have a custom UIView class. Inside that view, there is an image view.
Basically i have an application for Android 1.5 with a GLSurfaceView class that shows
I have 2 views that are sharing the same class files (.h and .m).
I have a class TouchInfo that has an ivar of class MotionStreak. @interface TouchInfo
I have come up with an idea such that on the touch of the
I have a signal and a slot that should fit together quite nicely. class
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is

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.