explicit-instantiation

How to explicitly instantiate a template for all members of MPL vector in C++?

时间秒杀一切 提交于 2019-12-03 04:56:00
问题 Consider the following header file: // Foo.h class Foo { public: template <typename T> void read(T& value); }; I want to explicitly instantiate the Foo::read member function template in a source file for all types included in a boost::mpl::vector : // Foo.cc #include <boost/mpl/vector.hpp> #include <boost/mpl/begin_end.hpp> #include "Foo.h" template <typename T> void Foo::read(T& value) { /* do something */ } typedef boost::mpl::vector<int, long, float> types; // template Foo::read<int >(int&

How to explicitly instantiate a template for all members of MPL vector in C++?

匆匆过客 提交于 2019-12-02 18:14:14
Consider the following header file: // Foo.h class Foo { public: template <typename T> void read(T& value); }; I want to explicitly instantiate the Foo::read member function template in a source file for all types included in a boost::mpl::vector : // Foo.cc #include <boost/mpl/vector.hpp> #include <boost/mpl/begin_end.hpp> #include "Foo.h" template <typename T> void Foo::read(T& value) { /* do something */ } typedef boost::mpl::vector<int, long, float> types; // template Foo::read<int >(int&); // template Foo::read<long >(long&); // template Foo::read<float>(float&); // instantiate

Member function template of class template can't find definition despite explicit instantiation present. Doesn't link

帅比萌擦擦* 提交于 2019-12-02 12:02:10
问题 Edit: This is not a duplicate of the linked question since I am using explicit instantiation and only a specific type of member functions do not link (others do). The following code compiles but doesn't link and I don't understand why. It is explicitly instantiating the Vector class to limit the number of possible arguments for T and therefore hides the definition of Vector<T> in a .cpp file. // fwd_decl.hpp #pragma once template<typename T> struct Vector; // Forward declare Vector to be used

Separate compilation and template explicit instantiation

隐身守侯 提交于 2019-12-01 11:01:42
Summary This question is about achieving separate compilation of a single template class instantiation in a several different translation units. Question For non-template classes one can put definitions in several .cpp files and compile them separately. For instance: file A.h: class A { public: void func1(); void func2(); void func3() { /* defined in class declaration */} } file A1.cpp: void A::func1() { /* do smth */ } file A2.cpp: void A::func2() { /* do smth else */ } Now I tried to do something similar with template classes. Since I know exactly which instances I will need, I'm explicitly

Separate compilation and template explicit instantiation

[亡魂溺海] 提交于 2019-12-01 08:17:17
问题 Summary This question is about achieving separate compilation of a single template class instantiation in a several different translation units. Question For non-template classes one can put definitions in several .cpp files and compile them separately. For instance: file A.h: class A { public: void func1(); void func2(); void func3() { /* defined in class declaration */} } file A1.cpp: void A::func1() { /* do smth */ } file A2.cpp: void A::func2() { /* do smth else */ } Now I tried to do

How to use extern template

对着背影说爱祢 提交于 2019-11-30 01:05:27
问题 I've been looking through the N3291 working draft of C++0x. And I was curious about extern template. Section 14.7.3 states: Except for inline functions and class template specializations, explicit instantiation declarations have the effect of suppressing the implicit instantiation of the entity to which they refer. FYI: the term "explicit instantiation declaration" is standard-speak for extern template . That was defined back in section 14.7.2. This sounds like it's saying that if you use

Explicit instantiation of templated constructor for template class

假如想象 提交于 2019-11-29 09:34:14
I am uncertain if it is a bug in Clang 3.2 or a violation of C++03, but it appears that explicit instantiation of templated constructors for template classes fails, but explicit instantiation of templated member functions of template classes succeeds. For instance, the following compiles without a problem with both clang++ and g++: template<typename T> class Foo { public: template<typename S> void Bar( const Foo<S>& foo ) { } }; template class Foo<int>; template class Foo<float>; template void Foo<int>::Bar( const Foo<int>& foo ); template void Foo<int>::Bar( const Foo<float>& foo ); template

When would you use template explicit instantiation?

柔情痞子 提交于 2019-11-28 09:43:49
I've just been reading about template explicit instantiation: template struct MyStruct<long>; It was described as "quite rare", so under what circumstances would it be useful? One of the use cases is to hide definitions from the end-user. tpl.h : template<typename T> void func(); // Declaration tpl.cpp : template<typename T> void func() { // Definition } template void func<int>(); // explicit instantiation for int template void func<double>(); // explicit instantiation for double main.cpp #include "tpl.h" int main() { func<double>(); // OK func<int>(); // OK // func<char>(); - Linking ERROR }

Why does explicit template instantiation not break ODR?

馋奶兔 提交于 2019-11-27 19:31:24
问题 This question arised in the context of this answer. As I would expect, this translation unit does not compile: template <int Num> int getNum() { return Num; } template int getNum<0>(); template int getNum<0>(); // error: duplicate explicit instantiation of 'getNum<0>' int main() { getNum<0>(); return 0; } I understand this, I have tried to make the same explicit template instantiation twice. However, it turns out that, separating this into different units, it compiles: // decl.h template <int

When would you use template explicit instantiation?

早过忘川 提交于 2019-11-27 03:11:06
问题 I've just been reading about template explicit instantiation: template struct MyStruct<long>; It was described as "quite rare", so under what circumstances would it be useful? 回答1: One of the use cases is to hide definitions from the end-user. tpl.h : template<typename T> void func(); // Declaration tpl.cpp : template<typename T> void func() { // Definition } template void func<int>(); // explicit instantiation for int template void func<double>(); // explicit instantiation for double main