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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:37:30+00:00 2026-05-11T07:37:30+00:00

I tried three iterations of the following simple program. This is a highly simplified

  • 0

I tried three iterations of the following simple program. This is a highly simplified attempt to write a container-and-iterator pair of classes, but I was running into issues with incomplete types (forward declarations). I discovered that this was in fact possible once I templatized everything – but only if I actually used the template parameter! (I realized this by looking at the Google sparsetable code.)

Any hints explaining why the second works while the third doesn’t? (I know why the first one doesn’t work – the compiler needs to know the memory layout of the container.)

Thanks in advance.

// This doesn't work: invalid use of incomplete type. #if 0 struct container; struct iter {   container &c;   int *p;   iter(container &c) : c(c), p(&c.value()) {} }; struct container {   int x;   int &value() { return x; }   iter begin() { return iter(*this); } }; int main() {   container c;   c.begin();   return 0; } #endif  // This *does* work. template<typename T> struct container; template<typename T> struct iter {   container<T> &c;   T *p;   iter(container<T> &c) : c(c), p(&c.value()) {} }; template<typename T> struct container {   T x;   T &value() { return x; }   iter<T> begin() { return iter<T>(*this); } }; int main() {   container<int> c;   c.begin();   return 0; };  // This doesn't work either. #if 0 template<typename T> struct container; template<typename T> struct iter {   container<int> &c;   int *p;   iter(container<int> &c) : c(c), p(&c.value()) {} }; template<typename T> struct container {   int x;   int &value() { return x; }   iter<int> begin() { return iter<int>(*this); } }; int main() {   container<int> c;   c.begin();   return 0; } #endif 
  • 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. 2026-05-11T07:37:30+00:00Added an answer on May 11, 2026 at 7:37 am

    The first requires a definition of container since you are doing a copy operation. If you define the constructor of iter after container‘s definition you’d be okay. So:

    struct container; struct iter {   container &c;   int *p;   iter(container &c); };  struct container {   int x;   int &value() { return x; }   iter begin() { return iter(*this); } };  iter::iter(container &c) : c(c), p(&c.value()) {}  int main() {   container c;   c.begin();   return 0; } 

    The second example works because there is no class until you actually instantiate one in your main function. By that time all types are defined. Try moving any of the iter or container templates definition after main and you’ll hit an error.

    The third example is a specialization for int or so it appears. This should compile because the template parameter for iter is not used. You’ve got the specialization syntax a bit off. However, there is no proper constructor so you’ll only get garbage for x. Moreover, iterators are modeled well by pointers. Passing this‘s value will not be of much help. Iterators are typically required for a sequence and not an individual object. Though, there is nothing that can stop you from building one.

    And you don’t need a ; after a function body.

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

Sidebar

Related Questions

I have tried for three days now and gotten nowhere on this.... I absolutely
I tried to run the following command and it says there is nothing to
In the following code I have to reuse the Active Pattern result three times
I've been trying to do the following: #[...] def __history_dependent_simulate(self, node, iterations=1, *args, **kwargs):
The following string of mine tried to find difference between two strings. But it's
Consider the following simplified example and desired output: class A { class combined_iterator {
Is there anyone who has already tried to use the Microsoft Bing translator web
I tried doing a mvn dependency:tree and I get a tree of dependencies. My
I've tried looking around and there are similar problems, but mine is way more
Is there any python module to convert PDF files into text? I tried one

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.