Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!

    2. News

    1. Red Dead Redemption 2

    1. GTA Online

      1. After Hours
      2. Find Lobbies & Players
      3. Guides & Strategies
      4. Vehicles
      5. Content Creator
      6. Help & Support
    2. Crews

      1. Events
      2. Recruitment
    1. Grand Theft Auto Series

    2. GTA Next

    3. GTA V

      1. PC
      2. Guides & Strategies
      3. Help & Support
    4. GTA IV

      1. Episodes from Liberty City
      2. Multiplayer
      3. Guides & Strategies
      4. Help & Support
      5. GTA Mods
    5. GTA Chinatown Wars

    6. GTA Vice City Stories

    7. GTA Liberty City Stories

    8. GTA San Andreas

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    9. GTA Vice City

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    10. GTA III

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    11. Top Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    12. Wiki

      1. Merchandising
    1. GTA Modding

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    3. Featured Mods

      1. DYOM
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Red Dead Redemption

    2. Rockstar Games

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Programming
      5. Movies & TV
      6. Music
      7. Sports
      8. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Forum Support

    2. Site Suggestions

K^2

Weekly C++ Challenge #1 - Template Virtuals

Recommended Posts

K^2

I don't know if I'll have enough steam or gather enough interest to really keep this weekly, but lets try.

 

I'm going to try and keep all of these challenges moderately practical, compliant with C++ standards, and hopefully interesting. None of them will require extensive knowledge of algorithms or large amounts of code to solve. Just various non-trivial ways to use the language.

 

 

Without further distraction, lets jump into this week's challenge. Anyone who has worked with or studied C++ had to deal with virtual functions. They can be fantastic ways to distinguish derived classes. For example, I might have a base class Foo with a virtual method Foo::IsInt() that tells me if some derived class is specialized to deal with integer types. But what if derived class is templated? What if you want to be able to distinguish between many base types a function could be using? Wouldn't it be fantastic if we could just call Foo::IsType<int>() instead? Unfortunately, C++ forbids declaring template member function virtual. And yet, this is precisely what I want you to overcome in this challenge. Write implementations for base class Foo and template class Bar derived from it which support the following code.

 

 

#include <iostream>class Foo;template <typename T>class Bar;void CheckInt(Foo* x){	if (x->IsType<int>())		std::cout << "is int.\n";	else		std::cout << "is not int.\n";}int main(){	Bar<int> x;	Bar<float> y;	std::cout << "x ";	CheckInt(&x);	std::cout << "y ";	CheckInt(&y);	return 0;}
Desired output.

 

x is int.y is not int.
Small hint.

 

Template specialization is done at compile time. Virtual functions are selected at run time. If there was only a way to bridge that gap...

 

 

Bigger hint.

 

Static variables inside template functions get initialized for each template initialization and will persist through run time.

 

 

Solution.

 

While there are several ways, in theory, to get type information at run time, by far the simplest, and perhaps most elegant, is to simply have a template function that generates unique type identifiers.

 

 

class Foo{    public:	template <typename T>	bool IsType()	{		return IsTypeImpl(GetTypeId<T>());	}	virtual bool IsTypeImpl(size_t id) = 0;    protected:	static size_t GetNextTypeId()	{		static size_t id = 0;		return id++;	}	template <typename T>	size_t GetTypeId()	{		static size_t id = GetNextTypeId();		return id;	}};template <typename T>class Bar : public Foo{    public:	bool IsTypeImpl(size_t id)	{		return id == GetTypeId<T>();	}};

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.