• Create Account

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

#1Prune  Members

223
Like
0Likes
Like

Posted 18 March 2014 - 09:35 PM

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, 18 March 2014 - 09:37 PM.

"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)

#2frob  Moderators

41387
Like
2Likes
Like

Posted 18 March 2014 - 11:51 PM

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?

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

#3Álvaro  Members

20268
Like
0Likes
Like

Posted 19 March 2014 - 02:28 AM

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.

#4Prune  Members

223
Like
0Likes
Like

Posted 19 March 2014 - 12:28 PM

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, 19 March 2014 - 12:31 PM.

"But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?" --Mark Twain

~~~~~~~~~~~~~~~Looking for a high-performance, easy to use, and lightweight math library? http://www.cmldev.net/ (note: I'm not associated with that project; just a user)

#5Álvaro  Members

20268
Like
0Likes
Like

Posted 19 March 2014 - 12:45 PM

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.

#6Javier Meseguer de Paz  Members

375
Like
0Likes
Like

Posted 19 March 2014 - 12:58 PM

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, 19 March 2014 - 12:59 PM.

“We should forget about small efficiencies, say about 97% of the time; premature optimization is the root of all evil” -  Donald E. Knuth, Structured Programming with go to Statements

"First you learn the value of abstraction, then you learn the cost of abstraction, then you're ready to engineer" - Ken Beck, Twitter

#7frob  Moderators

41387
Like
1Likes
Like

Posted 19 March 2014 - 01:26 PM

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.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

#8Javier Meseguer de Paz  Members

375
Like
0Likes
Like

Posted 19 March 2014 - 08:16 PM

If there is a must-read for C++ programmers, this FAQ is it.

“We should forget about small efficiencies, say about 97% of the time; premature optimization is the root of all evil” -  Donald E. Knuth, Structured Programming with go to Statements

"First you learn the value of abstraction, then you learn the cost of abstraction, then you're ready to engineer" - Ken Beck, Twitter

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.