• 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
Prune

std::chrono arguments best practices when one doesn't want function definitions in the header files

7 posts in this topic

duration and time_point are templated, which creates a problem for me as some of my (library) functions which take them as arguments are large and I put their definitions in cpp files rather than h files (another reason I do this sometimes is to decrease header file depencies). What's the best way to handle the obvious compile problem other than explicit instantiations (which is impractical because there are way too many reasonable combinations of even just the two template parameters)? Right now I simply fix the template parameters to the ones corresponding to the clock we use, but in some cases said function may pass some of these arguments to other functions such as the timed lock of an std mutex, and in general seems a rather unsatisfactory way to deal with this.

Edited by Prune
0

Share this post


Link to post
Share on other sites
I'm sorry, after reading that I'm having a hard time trying to figure out the actual problem you are having. The only question in there "What's the best way to handle the obvious compile problem other than explicit instantiations?" doesn't really make sense.

You describe putting their definitions inside .cpp instead of the headers. Templates don't work that way. In order to expand the templates inside the compilation unit the entire contents of the template must be available for expansion. If they aren't present, the compiler won't work.

Templates are a feature of the C++ language. Like nearly all features, it also has a cost when you use it. One cost of template code is that you must make the code available inside a compilation unit, usually through a large and relatively complex header, so the compiler can evaluate, process, and generate the necessary code. Another cost of templates is increased compile times, as templates need to be evaluated, processed, generated, and generally get inlined, elided, and optimized, usually all of this as separate work from the more traditional compilation steps. If you use them, you must pay the cost.


Other than "I don't want to do it that way", what specific problems are you trying to solve?
2

Share this post


Link to post
Share on other sites

duration and time_point are templated, which creates a problem for me as some of my (library) functions which take them as arguments are large and I put their definitions in cpp files rather than h files (another reason I do this sometimes is to decrease header file depencies).


I don't follow. std::string is a template (it's actually std::basic_string<char>), but that doesn't mean I need to put any code that uses strings in the header file, unless I am trying to support basic_string<T> for other types T.

How is your situation different?
0

Share this post


Link to post
Share on other sites

You describe putting their definitions inside .cpp instead of the headers. Templates don't work that way.

That's not strictly true.

// foo.h

template<typename T>
class Foo
{
	void Crap(T bar);
};

extern template Foo<int>;
extern template Foo<float>;


// foo.cpp

template<typename T>
void Foo<T>::Crap(T bar)
{
	...
}

template Foo<int>;
template Foo<float>;

In any case, I think you misunderstood my question. It isn't in regards to the technical workings of templates; I'm obviously familiar with that. It's about design/software engineering. Álvaro made the analogy with the basic_string class in his post, but that's not a good comparison, because in the vast majority of cases, basic_string is used with char as the template parameter, and in virtually all of the rest, with wchar_t. Thus, two possible instantiations cover essentially all use cases. With the chrono types, this is not so, as there are many reasonable combinations of the two template parameters for each of duration and time_point. So when writing parts of a library, it's not reasonable to explicitly instantiate all possible reasonable combinations as in the example code above. So you say, just have everything in the header file. But isn't it recommended practice to not put all implementation in header files? This isn't simply about the size of header files; putting some functions in the headers also reverses any attempts that would properly have been made at reducing header file interdepencency chains by opaque pointers, forward declarations, etc. I know it's not possible to have one's cake and eat it too in this case, but I'm simply looking for input about the best ways to work around these issues.

Edited by Prune
0

Share this post


Link to post
Share on other sites

The C++98 standard introduced the `export' keyword precisely for this purpose. Unfortunately, implementing this feature didn't mesh well with the traditional structure of compiling to object files and then linking, so popular compilers didn't implement it and it was removed in the C++11 standard.

 

I would bite the bullet and leave the implementation in the header file. I have seen people including a file from the header file that contains the implementation that shouldn't be there (I think they used .inc as its extension), but this is just an organizational tool.

0

Share this post


Link to post
Share on other sites

 

You describe putting their definitions inside .cpp instead of the headers. Templates don't work that way.

That's not strictly true.

// Code //

 

That's Explicit Instantiation, precisely.

 

As I imagine you know, if you want to use the template with a type for which it has not bee explicitly instantiated you won't be able to.

 

I am with Alvaro on this one, can't your functions just take specialized std::chrono::duration and std::chrono::time_point ?

 

If you can't, I guess there is not much you can do other than the explicit instantiation that you don't want to do. What I do, in case you are instested, is following this pattern with templates to separate the header and the... well, implementation:


//// template_name.h ////

#ifndef TEMPLATE_NAME_H
#define TEMPLATE_NAME_H

template <class T>
class TemplateName {
  public:
    void method1();
    void method2();
    // ...
};

#include "template_name-imp.h"

#endif /* TEMPLATE_NAME_H */


//// template_name-imp.h ////

#ifndef TEMPLATE_NAME_H 
#error "Please, do not include this file directly"
#endif

template <class T>
void TemplateName::method1() {/* ... */ }
void TemplateName::method2() {/* ... */ }

//// any_other_cpp.cpp

// ...

#include "template_name.h"

// ...

This of course completely defeat the purpose of reducing header dependency, but keeps the code organized.

Edited by Javier Meseguer de Paz
0

Share this post


Link to post
Share on other sites


So you say, just have everything in the header file. But isn't it recommended practice to not put all implementation in header files?

 

Normally that is true.  

 

Normally the .h file contains the declaration; it states the interface so that other code can reference the object.  

Normally the .cpp file contains the definitions and instances and other concrete elements.

 

However, this does not apply to templates, because a template is not a class or a function. A template is something altogether different.

 

A template is neither of those things. It is a template, a pattern, a cookie cutter, a fancy compiler command to generate code.  A template is used to generate a family of classes or a family of functions.  By itself it is nothing; it provides a pattern that the compiler can use to generate code at compile time.

 

The rules in the physical world apply just as well to the compiler world. You cannot provide a part of a cookie pattern, or a part of a dress pattern, or a part of a class pattern, or a part of a function pattern, and expect the tools to fill in the blanks.

 

In order for a compiler to use the template it must be fully included.  As Alvaro suggested your header file could include a second file that includes those details, but it doesn't change the fact that the entire template must be visible inside the compilation unit.

1

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