explicit-instantiation

Constrained member functions and explicit template instantiation

泪湿孤枕 提交于 2021-02-07 01:21:45
问题 G++ and Clang++ agree that the following snippet is not valid C++: template<int dim, int rank> struct Tensor {}; template<int dim> double InnerProduct(Tensor<dim, 1> const &, Tensor<dim, 1> const &) { return 0.0; } template<int dim> double DoubleInnerProduct(Tensor<dim, 2> const &, Tensor<dim, 2> const &) { return 0.0; } template<int dim, int rank> class Field { private: static double Dot(Tensor<dim, rank> const &u, Tensor<dim, rank> const &v) requires (rank == 1) { return InnerProduct(u, v);

Explicit instantiation of member function template of class template

戏子无情 提交于 2019-12-23 18:34:10
问题 Supposing I have a class template in my header file with a member function template. //file.hxx template<class T> struct A { T val; template<class U> foo(U a); }; and I have in a .cpp the implementation of foo: //file.cpp #include "file.hxx" #include <typeinfo> template<class T> template<class U> A<T>::foo<U>(U a){ std::cout << "Type T: " << typeid(val).name() << std::endl; std::cout << "Type U: " << typeid(a).name() << std::endl; } If I want to explicitly instantiate my class and member

Explicit instantiation of templated constructor for template class

╄→гoц情女王★ 提交于 2019-12-18 05:45:05
问题 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

How do you force a templatization to match a base class?

大城市里の小女人 提交于 2019-12-10 12:14:32
问题 I have a template function which is explicitly instantiated for Base class, but not for Derived class. How can I force the uses that pass a Derived class (or other derived classes) to match against the Base class? Header file: class Base { }; class Derived : public Base { }; class Derived2 : public Base { }; template <typename Example> void function(Example &arg); Implementation file: // Explicitly instantiate Base class: template void function<Base>(Base &arg); // Define the template

Separating definition/instantiation of template classes without 'extern'

被刻印的时光 ゝ 提交于 2019-12-08 07:27:28
问题 The (not so new anymore) C++11 standard introduced the extern keyword for templates. Its purpose is to tell the compiler that a template should not be instantiated at the point of usage, but that it will be instantiated in another translation unit (and thus there will be an instantiation available at link time) - at least AFAIK. Now, even in the pre-C++11 era we used something similar to separate the declaration/definition of template classes from its instantiation in order to speed up