specialization

Partial specialization for one method in the class

倾然丶 夕夏残阳落幕 提交于 2021-02-11 12:23:23
问题 I have a template matrix class. I try to implement the size (always square) as a template parameter. template< // Type of data typename type_t, // dimension of the matrix 4 -> 4x4 std::size_t dim_t, // I don't want a matrix of non numeric value typename = typename std::enable_if_t< std::is_arithmetic_v< type_t >, type_t > > class Matrix final { // .... } With this code, I would like to be able to make a different method for different sizes of matrix. Because some method force me to take it

Why does this dependent type not count as specialization using the template argument?

我怕爱的太早我们不能终老 提交于 2021-01-27 05:27:08
问题 I'm trying to group specializations together to avoid writing them multiple times. For example, in the below code, I try to specialize "float" and "double" as one case of implementation for foo::func(); I then use another implementation for "bool." template<typename T> struct foo; template<typename T> struct bar; template<> struct bar<float> { typedef float Type; }; template<> struct bar<double> { typedef double Type; }; /* specialize for float and double here */ template<typename T> struct

Why does this dependent type not count as specialization using the template argument?

痴心易碎 提交于 2021-01-27 05:25:00
问题 I'm trying to group specializations together to avoid writing them multiple times. For example, in the below code, I try to specialize "float" and "double" as one case of implementation for foo::func(); I then use another implementation for "bool." template<typename T> struct foo; template<typename T> struct bar; template<> struct bar<float> { typedef float Type; }; template<> struct bar<double> { typedef double Type; }; /* specialize for float and double here */ template<typename T> struct

Swift protocol specializing generic protocol

流过昼夜 提交于 2020-02-08 07:34:12
问题 Is it possible to have a protocol that specializes a generic protocol? I want something like this: protocol Protocol: RawRepresentable { typealias RawValue = Int ... } This does compile, but when I try to access the init or rawValue from a Protocol instance, its type is RawValue instead of Int . 回答1: In Swift 4, you can add constraints to your protocol: protocol MyProtocol: RawRepresentable where RawValue == Int { } And now all methods defined on MyProtocol will have an Int rawValue. For

Explicit instantiation of function template specialization

元气小坏坏 提交于 2020-01-04 15:13:15
问题 I am trying to create a global function template specialized for some given types. It looks something like that: A.h (primary template, template specialization, extern) template <typename T> void foo() { std::cout << "default stuff" << std::endl; } template<> void foo<int>() { std::cout << "int stuff" << std::endl; } extern template void foo<int>(); A.cpp (explicit instantiation) template void foo<int>(); B.h void bar(); B.cpp (includes A.h) void bar() { foo<int>(); } main.cpp foo<int>(); bar

template function specialization default argument

让人想犯罪 __ 提交于 2020-01-04 13:43:44
问题 template <typename T> void function(T arg1, T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max()) { } template <> void function<int>(int arg1, int min,int max) { } int main(int argc,char* argv[]) { function<int>(1); } it give syntax error C2689 and C2059 on function default argument line on :: token. but without specialization, it doing fine. and if I change default argument and still doing specialization: template <typename T> void function(T arg1, T min = T(0), T max

template function specialization default argument

守給你的承諾、 提交于 2020-01-04 13:42:48
问题 template <typename T> void function(T arg1, T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max()) { } template <> void function<int>(int arg1, int min,int max) { } int main(int argc,char* argv[]) { function<int>(1); } it give syntax error C2689 and C2059 on function default argument line on :: token. but without specialization, it doing fine. and if I change default argument and still doing specialization: template <typename T> void function(T arg1, T min = T(0), T max

How does trait specialization actually work?

别来无恙 提交于 2020-01-01 17:09:14
问题 I tried to specialize a trait, and it fails to compile because of "conflicting implementations". But my understanding of specialization is that more specific implementations should override more generic ones. Here is a very basic example: mod diving { pub struct Diver<T> { inner: T } } mod swimming { use diving; pub trait Swimmer { fn swim(&self) { println!("swimming") } } impl<T> Swimmer for diving::Diver<T> { } } mod drowning { use diving; use swimming; impl swimming::Swimmer for diving:

Why are function template specializations not allowed inside a class?

▼魔方 西西 提交于 2020-01-01 02:19:34
问题 After having found answers to many of my questions on stackoverflow, I have now come up against a question of which I can't find the answer and I hope that someone is willing to help me! My problem is that I want to do an explicit templatization of a function inside a class in C++. My compiler (g++) and a look in the C++ standard (§14.7.3) tells me that this specialization has to be done in the namespace in which the class is declared. I understand that this implies that I cannot put the

template class specialization with template class parameter

◇◆丶佛笑我妖孽 提交于 2019-12-30 11:11:18
问题 Say I have : template < typename T > class ClassA { void doTheStuff (T const * t); }; template < typename T > class ClassB { // Some stuff... }; I'd like to specialize the doTheStuff method for all instances of the ClassB template like this: template <typename T> void ClassA< ClassB< T > >::doTheStuff (ClassB< T > const * t) { // Stuff done in the same way for all ClassB< T > classes } But of course, this doesn't work. Shame is I don't know how I could do that. With visual studio's compiler I