Field alone is not a type, but a template which can generate a family of types, such as Field<int> and Field<double>. All these fields are not related such that the one is somehow derived from the other or such. So you have to establish some relation between all these generated types. One way is to use a common non-template base class:

class FieldBase { };

template <typename T>
class Field : public FieldBase {
  private:
    T value;
    DataType<T> type;
};
class Row {
  private:
    std::map<unsigned long,FieldBase*> column;
}; 

And consider using smart pointer instead of that raw pointer in the code. Anyway, now the problem is that the type-information is lost - whether you point to a Field<double> or to a Field<int> is not known anymore and can only be detected by keeping some sort of type-flag in the base which is set by the templated derived class - or by asking RTTI using

dynamic_cast<Field<int>*>(field) != 0

But that's ugly. Especially because what you want there is a value semantic. I.e you would want to be able to copy your row, and it would copy all the fields in it. And you would want to get a double when a double is stored - without first using RTTI to hack your way to the derived type.

One way of doing it is to use a discriminated union. That is basically an union for some arbitrary types and in addition a type-flag, which stores what value is currently stored in that field (e.g whether a double, int, ...). For example:

template <typename T>
class Field {
  private:
    T value;
    DataType<T> type;
};
class Row {
  private:
    std::map<unsigned long, 
             boost::variant< Field<int>, Field<double> > > 
      column;
};

boost::variant does all the work for you. You can use visitation to make it call a functor using the right overload. Have a look at its manual

Answer from Johannes Schaub - litb on Stack Overflow
🌐
Teachers Pay Teachers
teacherspayteachers.com › browse › free
Classroom Map Template | TPT
Key Features:✨Fully Editable: Customize for any class or subject. ✨Mon ... template, pacing guide and scope and sequence calendar with links to 180+ days of reading, writing and grammar lessons for middle school or high school English Language Arts to teach EVERY skill with highly engaging, fun no prep resources that are easily adaptable to any skill level. Enjoy No Prep secondary ELA planning. PDF with editable Google Doc Link. Want every product in this complete curriculum · map...
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › map-associative-containers-the-c-standard-template-library-stl
Map in C++ STL - GeeksforGeeks
The map container is defined as std::map class template inside the <map> header file.
Published   January 17, 2026
🌐
Perusall
teaching-resources.delta.ncsu.edu › use-a-course-map-template
Use a Course Map Template | Teaching Resources
You may now edit your copy of the template to reflect your own course. Use Bloom’s taxonomy to write course learning objectives and module learning objectives · Use one measurable verb for each learning objective · Use a consistent numbering system to indicate alignment between learning objectives and: ... View an example course map from HS 201.
Top answer
1 of 3
29

Field alone is not a type, but a template which can generate a family of types, such as Field<int> and Field<double>. All these fields are not related such that the one is somehow derived from the other or such. So you have to establish some relation between all these generated types. One way is to use a common non-template base class:

class FieldBase { };

template <typename T>
class Field : public FieldBase {
  private:
    T value;
    DataType<T> type;
};
class Row {
  private:
    std::map<unsigned long,FieldBase*> column;
}; 

And consider using smart pointer instead of that raw pointer in the code. Anyway, now the problem is that the type-information is lost - whether you point to a Field<double> or to a Field<int> is not known anymore and can only be detected by keeping some sort of type-flag in the base which is set by the templated derived class - or by asking RTTI using

dynamic_cast<Field<int>*>(field) != 0

But that's ugly. Especially because what you want there is a value semantic. I.e you would want to be able to copy your row, and it would copy all the fields in it. And you would want to get a double when a double is stored - without first using RTTI to hack your way to the derived type.

One way of doing it is to use a discriminated union. That is basically an union for some arbitrary types and in addition a type-flag, which stores what value is currently stored in that field (e.g whether a double, int, ...). For example:

template <typename T>
class Field {
  private:
    T value;
    DataType<T> type;
};
class Row {
  private:
    std::map<unsigned long, 
             boost::variant< Field<int>, Field<double> > > 
      column;
};

boost::variant does all the work for you. You can use visitation to make it call a functor using the right overload. Have a look at its manual

2 of 3
2
  1. You got an error there: you have to "value" member in Field (one should probably be "type").
  2. Please don't keep raw pointers in the map's value. Use boost::shared_ptr.
  3. Also, you should have a good reason for writing such classes where there are plenty of DB/table handling code out there already which you can probably use. So, if it's applicable, consider using something existing and not writing your own table handling code.

Now, to answer your question :), the Field<> classes can inherit from a common base class that's shared by all data types. This way a container such as your column map can keep pointers (make that shared pointers) to derived objects that are instanced of a template class.

🌐
Psu
facdev.e-education.psu.edu › plan › mapping
Creating a Course Map - Faculty Development - Penn State
GEOG 581 Course Blueprint (map) created in Microsoft Excel (there is also a Microsoft Excel template available)
🌐
Course Map Guide
coursemapguide.com
The Online Course Map Guide
The Online Course Mapping Guide is a walk-through of the course mapping process. This site is designed to help you build a vision and a map for your course. The guided exercises in this site will help you identify course competencies and create learning outcomes that provide a foundation for ...
Top answer
1 of 3
1

What do you reckon this constructor does? Adding one value at the beginning of map?

It initialises the map so that map[x] == v for any x. The map associates intervals with values, internally storing a normal map keyed by the start of each interval; it's initialised so that the entire range of the key type maps to the initial value.

I see though in the respective key only an address as value after initializing in main. What is wrong? The operator [] is supposed to get the values for a specific key. However I cannot use it so as to get the elements of the map in the output. Any hint?

I've no idea what you're asking there. If you try, for example, cout << Map1[42] << '\n';, then your program should output 10, since that is the initial value assigned to the entire range of integers.

Consider also that it should be a function that inserts values to the map.

Since the internal map is publicly exposed, you can add a new interval to the map with

Map1.my_map.insert(std::make_pair(interval_start, value));

It might be more polite to make my_map private, and provide an insert() function to do that. You could also add a non-const overload of operator[] that inserts a new range and returns a reference to its value, something like

V & operator[](K const & key) {
    V const & old_value = (--my_map.upper_bound(key))->second;
    return *my_map.insert(std::make_pair(key, old_value)).first;
}

although this might not be a great idea, as you'd have to be careful that you don't accidentally insert many ranges when you only want to read the values.

My problem is how to iterate through the map to get all its elements and print them in main. It shows me an address with a value of the object initialization.

Remembering that an iterator over a map refers to a key/value pair (of type std::pair<K,V>), you should be able to iterator over the map like this:

for (auto it = Map1.begin(); it != Map1.end(); ++it) {
    std::cout << it->first << " maps to " << it->second << '\n';
}

(in C++03, you'll need to write template_map<int,int>::const_iterator rather than auto).

2 of 3
1

What do you reckon this constructor does? Adding one value at the beginning of map? I see though in the respective key only an address as value after initializing in main. What is wrong?

It adds this one value in to the map. The iterator argument is only a hint: if the new item is to be inserted right after this position, the operation can be completed faster. Otherwise the map will need to find the right place to insert the new value as usual.

The operator [] is supposed to get the values for a specific key. However I cannot use it so as to get the elements of the map in the output. Any hint?

upper_bound returns iterator to the first key-value pair, where key is greater than the argument. --upper_bound therefore returns an iterator to the item, whose key is either equal or less than the queried key. If upper_bound returned map.begin(), because all keys are greater than the query, decrementing it is undefined behavior.

What you need here is the find member function. You also need to deal with the case the key is not found (map.end() is returned), e.g by throwing an exception.

Alternatively you may implement your operator[] in terms of map::operator[]. This means that the function can't be const, because map inserts a new default value if the key is not found.

Find elsewhere
🌐
Cplusplus
cplusplus.com › forum › general › 201936
Map to store templated base class. - C++ Forum
If BaseClass is a template you cannot use it whitout specializing, i.e. providing the template parameter. Further more this way you would store copies of the base class only and all information regarding the derived class are lost.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › mfc › how-to-create-a-message-map-for-a-template-class
How to: Create a Message Map for a Template Class | Microsoft Learn
August 3, 2021 - The BEGIN_TEMPLATE_MESSAGE_MAP macro was designed to allow classes containing a single template argument to declare their own message maps.
🌐
Icograms
icograms.com › usage-school-maps
School Map. Design Comprehensive School Maps with Icograms Designer | Enhance Navigation and Planning.
Create detailed and customizable school maps, outdoor views, and indoor layouts effortlessly with Icograms Designer. Streamline navigation, enhance planning, and provide a visual representation of your school's campus or building.
🌐
UCATT
ucatt.arizona.edu › news › course-map-templates
Course Map Templates - UCATT - The University of Arizona
July 20, 2021 - Have you ever wondered what a course map is or looks like? It's a road map or blueprint for building your course, and outlines the alignment of learning outcomes, assessments and assignments. Download and use one of these templates to kickstart your course development!
🌐
Classmap
classmap.org
ClassMap - Create special education schedules
Tools to aid special education teachers in creating master schedule and generating individual student schedules.
🌐
Course Map Guide
coursemapguide.com › mapping-your-course
Mapping Your Course | Course Map Guide
Download a course map template and learn the steps of backward design in developing your online course.
🌐
Twinkl
twinkl.com › resource › sg-c-34-map-of-my-classroom-activity-sheet
Make a Map of Your Classroom - Classroom Map Worksheet
Instantly access Twinkl's printable and digital K-12 teaching resources, including worksheets, eBooks, games, PowerPoints, Google Slides, and more!
🌐
Cisco
cisco.com › c › en › us › td › docs › routers › sdwan › command › iosxe › qualified-cli-command-reference-guide › m-class-map.pdf pdf
Class-Map Commands • class-map, on page 1 • match qos-group, on page 3
Class-map configuration (config-cmap) Command History · Modification · Release · Command qualified for use in Cisco vManage CLI · templates. Cisco IOS XE Catalyst SD-WAN Release 17.2.1v · Examples · class-map match-any BestEffort · match qos-group 3 ·
🌐
Teachersprintables
teachersprintables.net › preview › Curriculum_Map_Template
Curriculum Map Template
A twist on a lesson plan, this curriculum map template sets teachers up to focus on a big-picture idea or specific learning outcome. It is ideal for elementary grades, and visualizes up to four months or an entire semester at a time. Free to download and print
🌐
Cplusplus
cplusplus.com › forum › beginner › 109292
making a variety map using templates - C++ Forum
Although this is completely useless, since it's a needless wrapper around the std::map constructor. Something with a little more substance to it would be: Although again, this is a simple wrapper. (Take this code with a grain of salt, I haven't codded in a while and don't have a compiler handy) EDIT: Concerning your actual question, you could look into creating a class which wraps up a union of all the types you want to support, and an enumeration which describes those types.
🌐
Teachers Pay Teachers
teacherspayteachers.com › browse › free
Curriculum Map Template | TPT
Key Features:✨Fully Editable: Customize for any class or subject. ✨Mon ... template, pacing guide and scope and sequence calendar with links to 180+ days of reading, writing and grammar lessons for middle school or high school English Language Arts to teach EVERY skill with highly engaging, fun no prep resources that are easily adaptable to any skill level. Enjoy No Prep secondary ELA planning. PDF with editable Google Doc Link. Want every product in this complete ... map ...
🌐
Pinterest
pinterest.com › explore › education › classroom map
Classroom Map
Map Activities For Kids · See more · Perfect Classroom Layout · Classroom Set Up Ideas Layout Preschool · Preschool Classroom Layout Ideas · Primary Classroom Layout · Editable Classroom Layout · School Room Layout · Dream Classroom Layout · Effective Classroom Layout · Pre K Classroom Layout · Perfect Classroom Layout · Classroom Floor Plan Template ·