template-specialization

Partial template specialization for initialization of static data members of template classes

前提是你 提交于 2019-12-08 12:28:44
问题 How would one initialize static data members of a template class differently for particular parameters? I understand that templates are different than other kinds of classes and only what is used in the project ever gets instantiated. Can I list a number of different initializations for different parameters and have the compiler use whichever is appropriate? For example, does the following work, and if not what is the correct way to do this? : template<class T> class someClass { static T

C++: Manual disambiguation of partial specialization (with SFINAE)

我与影子孤独终老i 提交于 2019-12-08 09:52:32
问题 I am implementing a generic class, which should behave differently for different sets of types (not only for different discrete types). The goal is to serialize objects of different types to send them over custom protocol (but it is more educational task rather than something pragmatical; I am a student that is interested in distributed computing). For example, I need to send floats and integers differently. I also want to have a default handler of other POD types. But I need to override the

Template Specialization for each Range Type

隐身守侯 提交于 2019-12-08 09:48:02
问题 Background In C++11 the range-based for loop handles three kinds of "ranges," outlined here (link). I've quoted the relevant part below. Syntax for (range_declaration : range_expression) loop_statement Explanation The above syntax produces code similar to the following ( __range , __begin and __end are for exposition only): { auto && __range = range_expression; for (auto __begin = begin_expr, __end = end_expr; __begin != __end; ++__begin) { range_declaration = *__begin; loop_statement } } The

Visibility of template function specialisations in shared libraries; dependence on template parameter

我只是一个虾纸丫 提交于 2019-12-08 08:00:34
I am building some C++ code that utilises pybind11 to do some wrapping and create a Python extension module, and I am encountering some undefined symbol problems. Upon further examination, it appears to be because certain template function specialisations are not publically visible in the shared library I am building. I have a MWE to demonstrate explicitly the problem: symbol_test.hpp #include <pybind11/pybind11.h> template <typename T> void test_func(T var) {} template <> void test_func<int>(int); template <> void test_func<pybind11::object>(pybind11::object); symbol_test.cpp #include "symbol

Visibility of template function specialisations in shared libraries; dependence on template parameter

烂漫一生 提交于 2019-12-08 06:35:40
问题 I am building some C++ code that utilises pybind11 to do some wrapping and create a Python extension module, and I am encountering some undefined symbol problems. Upon further examination, it appears to be because certain template function specialisations are not publically visible in the shared library I am building. I have a MWE to demonstrate explicitly the problem: symbol_test.hpp #include <pybind11/pybind11.h> template <typename T> void test_func(T var) {} template <> void test_func<int>

Static template member function for template class

孤街浪徒 提交于 2019-12-08 05:44:26
问题 I have a template class and a template member function: template<class T1> struct A{ template<class T2> static int f(){return 0;} }; I want to specialize for a case when T1 and T2 are the same, For example, define the case A<T>::f<T> for any T . but I can't find the combination of keywords to achieve this. How can I partially (?) specialize a combination of template class and a template static function? These are my unsuccessful attempts, and the error messages: 1) Specialize inside the class

Default values of template parameters in the class template specializations

最后都变了- 提交于 2019-12-07 17:31:59
问题 Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate { }; template <class x1, class x2> struct CoreTemplate<x1*, x2*> { int spec; CoreTemplate() { spec = 1; } }; template <class x> struct CoreTemplate<x*> { int spec; CoreTemplate() { spec = 3; } }; int main(int argc, char* argv[]) { CoreTemplate<int*, int*> qq1; printf("var=%d.\r\n", qq1.spec); CoreTemplate<int*> qq2; printf("var=%d.\r\n", qq2.spec); } MSVC compiles this code fine and selects the second

Specialize same operator for different traits

杀马特。学长 韩版系。学妹 提交于 2019-12-07 11:03:08
问题 I want to do the following with specialization by traits. Array Aa = Scalar in_a would use overload I . Array Aa = Array Bb would use overload II . In the following code, overload II never get used. Someone mentioned that T1 cannot be deduced in overload II . How to fix that? I used the C++ shell to compile the code with C++14. #include <iostream> #include <type_traits> using namespace std; class A; // forward declaration. template <typename T> struct is_A : false_type {}; template <> struct

How can I partially specialize a class template for ALL enums?

蓝咒 提交于 2019-12-07 05:23:37
问题 Say I have some class template: template<typename T> class { // .... } I can partially specialize this template for ALL pointers by: template<typename T> class<T *> { // .... } Can I somehow specialize the template for ALL enums? i.e., do something like: (this doesn't work, though) template<typename T> class<enum T> { // .... } 回答1: use C++11 and SFINAE. #include <type_traits> template<typename T, typename = void> struct Specialize { }; template<typename T> struct Specialize<T, typename std:

Function template specialization and the Abrahams/Dimov example

不想你离开。 提交于 2019-12-07 04:42:44
问题 (I'm assuming knowledge of the Abrahams/Dimov example in this question.) Assume there is some 3rd-party code in a header that like this, which you cannot modify: template<class T> void f(T); // (1) base template 1 template<class T> void f(T *); // (2) base template 2 template<> void f<>(int *); // (3) specialization of (2) The question is: If I have been given the declarations above as-is, is it possible for me to now specialize the base template 1 for the case where T = int * (for example)?