• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
zfvesoljc

[c++] template magic (template class with member function specialization)

8 posts in this topic

i'm trying to do create a template class and implement a specialized member function:
[code]
template<class T, class U>
class TFoobar {
public:
void func(T t, U u) { // generic }
void func(float t, U u); { // float spec }
}
[/code]


I tried a few things, but didn't manage to make it work (or compile properly for both msvc and gcc). Can I make this work like outlined in the above snippet, or must I make a class specialization?
0

Share this post


Link to post
Share on other sites
What you are looking for is partial specialization, not all compilers support it, but it is incresingly common on most used ones. Also you must understand that T may be float, so you would endup with 2 definitions of a single method (forbidden). Therefore in order to avoid that problem, check type_traits which will allow you to "use one or the other based on type of class T".

Cheers.
1

Share this post


Link to post
Share on other sites
[quote name='kilah' timestamp='1322073169' post='4886964']
What you are looking for is partial specialization, not all compilers support it, but it is incresingly common on most used ones. Also you must understand that T may be float, so you would endup with 2 definitions of a single method (forbidden).
[/quote]

yep, found that out


[size="2"][quote name='kilah' timestamp='1322073169' post='4886964']
Therefore in order to avoid that problem, check type_traits which will allow you to "use one or the other based on type of class T".
Cheers.
[/quote]

[/size][size="2"]so basically I just branch the code based on the is_floating_point(), or any other that is?[/size]
0

Share this post


Link to post
Share on other sites
There are several ways to use enable_if/disable_if in this case, but my way of solving this would be a disable_if on your first func when is_floating returns true for T. This could lead you to another problem: What about double types? You would end up with only func(float.... ), not the generic one, which may, or may not, be a problem for you in this case.
0

Share this post


Link to post
Share on other sites
[quote name='kilah' timestamp='1322088401' post='4887039']
There are several ways to use enable_if/disable_if in this case, but my way of solving this would be a disable_if on your first func when is_floating returns true for T. This could lead you to another problem: What about double types? You would end up with only func(float.... ), not the generic one, which may, or may not, be a problem for you in this case.
[/quote]

quote:

[b]is_floating_point[/b]
template <class _T> struct is_floating_point;[font="Arial, sans-serif"][size="2"]std::tr1::is_floating_point<T>::value == true if and only if T is one of the following types:[/size][/font]

[list][*][const] [volatile] float[*][const] [volatile] double[*][const] [volatile] long double[/list]
[font="Arial, sans-serif"] [/font]
[url="http://publib.boulder.ibm.com/infocenter/comphelp/v9v111/index.jsp?topic=/com.ibm.xlcpp9.aix.doc/standlib/header_type_traits.htm"]http://publib.boulde...type_traits.htm[/url]
0

Share this post


Link to post
Share on other sites
[quote name='zfvesoljc' timestamp='1322120185' post='4887149']
Ran into a problem, the type_traits is not implemented on the target platform...

Any other ideas?
[/quote]

Problem is you can't partially specialize functions, but you can partially specialize an implementation structure within TFoobar to get what you need

[code]template<class T, class U>
class TFoobar {
public:
template <class T, class U>
struct func_impl {
static void impl(T t, U u) { }
};

template <class U>
struct func_impl<float, U> {
static void impl(float t, U u) { }
};

void func(T t, U u) { func_impl<T, U>::impl(t, u); }};
[/code]
1

Share this post


Link to post
Share on other sites
How about...
[code]

template<typename T, typename U>
class Hi {
public:
void print(T) {
std::cout << "generic" << std::endl;
}
};

template<typename U>
class Hi<float, U> {
public:
void print(float) {
std::cout << "float" << std::endl;
}
};
[/code]

?
If the class is big enough that this causes a lot of duplicated code, maybe you can pull this functionality out into another class or function and use that one from the "main" class.
0

Share this post


Link to post
Share on other sites
[quote name='patrrr' timestamp='1322126533' post='4887168']
How about...
[code]

template<typename T, typename U>
class Hi {
public:
void print(T) {
std::cout << "generic" << std::endl;
}
};

template<typename U>
class Hi<float, U> {
public:
void print(float) {
std::cout << "float" << std::endl;
}
};
[/code]

?
If the class is big enough that this causes a lot of duplicated code, maybe you can pull this functionality out into another class or function and use that one from the "main" class.
[/quote]



yeah, that was the initial solution (and currently implemented), but I'll try the method IFooBar provided.
0

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
Sign in to follow this  
Followers 0