Template instantiation in cpp file




















Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. I have some template code that I would prefer to have stored in a CPP file instead of inline in the header. I know this can be done as long as you know which template types will be used.

For example:. Note the last two lines - the foo::do template function is only used with ints and std::strings, so those definitions mean the app will link.

I am only using this code with VS at the moment but will be wanting to port to other environments. The problem you describe can be solved by defining the template in the header, or via the approach you describe above. For others on this page wondering what the correct syntax is as did I for explicit template specialisation or at least in VS , its the following This code is well-formed. You only have to pay attention that the definition of the template is visible at the point of instantiation.

The definition of a non-exported function template, a non-exported member function template, or a non-exported member function or static data member of a class template shall be present in every translation unit in which it is explicitly instantiated. Your example is correct but not very portable. There is also a slightly cleaner syntax that can be used as pointed out by namespace-sid, among others.

Of course, you can have multiple implementations in the third file. Or you might want multiple implementation files, one for each type or set of types you'd like to use, for instance. This setup should reduce compile times, especially for heavily used complicated templated code, because you're not recompiling the same header file in each translation unit. It also enables better detection of which code needs to be recompiled, by compilers and build scripts, reducing incremental build burden.

If memory serves, I originally got the idea for this from SO. This should work fine everywhere templates are supported. That is a standard way to define template functions. I think there are three methods I read for defining templates. Or probably 4. Each with pros and cons. Define in class definition.

I don't like this at all because I think class definitions are strictly for reference and should be easy to read. However it is much less tricky to define templates in class than outside. And not all template declarations are on the same level of complexity. This method also makes the template a true template. Define the template in the same header, but outside of the class.

This is my preferred way most of the times. It keeps your class definition tidy, the template remains a true template. It however requires full template naming which can be tricky. Also, your code is available to all. But if you need your code to be inline this is the only way.

You can also accomplish this by creating a. INL file at the end of your class definitions. Include the header. CPP into your main. I think that's how its done. You won't have to prepare any pre instantiations, it will behave like a true template.

The problem I have with it is that it is not natural. We don't normally include and expect to include source files. I guess since you included the source file, the template functions can be inlined. This last method, which was the posted way, is defining the templates in a source file, just like number 3; but instead of including the source file, we pre instantiate the templates to ones we will need.

Asked 4 years, 7 months ago. Active 4 years, 7 months ago. Viewed times. How can I keep the separate header approach whilst also separating test and production code? Sam Brightman Sam Brightman 2, 4 4 gold badges 31 31 silver badges 30 30 bronze badges. You can have a "private" Foo.

Can you clarify what you mean? I mean in Foo. Maybe you could expand that into a full answer? Why does my method still fail to link? I don't want to include cpp , that why I introduce. Show 1 more comment. The constexpr if helps us to make this entire function template possible. With a normal, if this code would not compile.

It will not compile. We can fix this by providing an additional if for this case :. Now it compiles. But there is more. It also shows which if -branch is used in that instantiation. If you look closely you can spot something else.

There is just an if and a else. Why is this important? Because we need to apply the constexpr to all if -branches. Otherwise, we end up with a run-time if in a constexpr if else-branch. You can support the project by becoming a Patreon or of course with code contributions.

My special thanks to Embarcadero. My special thanks to PVS-Studio. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Related



0コメント

  • 1000 / 1000