template-specialization

std::remove_reference explained?

折月煮酒 提交于 2021-02-04 09:10:06
问题 I saw possible implementations for std::remove_reference as below template< class T > struct remove_reference {typedef T type;}; template< class T > struct remove_reference<T&> {typedef T type;}; template< class T > struct remove_reference<T&&> {typedef T type;}; Why is it that there are specializations for lvalue and rvalue reference ? Won't the general template itself be sufficient and remove the reference? I'm confused here because in the T& or T&& specialization if I try to use ::type I

std::remove_reference explained?

蓝咒 提交于 2021-02-04 09:05:53
问题 I saw possible implementations for std::remove_reference as below template< class T > struct remove_reference {typedef T type;}; template< class T > struct remove_reference<T&> {typedef T type;}; template< class T > struct remove_reference<T&&> {typedef T type;}; Why is it that there are specializations for lvalue and rvalue reference ? Won't the general template itself be sufficient and remove the reference? I'm confused here because in the T& or T&& specialization if I try to use ::type I

std::remove_reference explained?

六月ゝ 毕业季﹏ 提交于 2021-02-04 09:05:03
问题 I saw possible implementations for std::remove_reference as below template< class T > struct remove_reference {typedef T type;}; template< class T > struct remove_reference<T&> {typedef T type;}; template< class T > struct remove_reference<T&&> {typedef T type;}; Why is it that there are specializations for lvalue and rvalue reference ? Won't the general template itself be sufficient and remove the reference? I'm confused here because in the T& or T&& specialization if I try to use ::type I

Create gmock tests for template specialization methods

断了今生、忘了曾经 提交于 2021-01-29 19:56:07
问题 I want to add GMOCK tests to verify if the container accesses the correct method. For vector it should access the second method, and for set it should access the first method (because set has set.find ). This is my template specialization: namespace tools{ struct low_priority {}; struct high_priority : low_priority {}; template<class TSource, class Ty> auto exists_in(high_priority, const TSource &source, const Ty &item) -> decltype(source->find(item) != source.end()) { return source.find(item

How can I specialize an algorithm for iterators that point to complex values?

北战南征 提交于 2021-01-29 13:26:54
问题 I am trying to write an algorithm that works on iterators (similar to the STL algorithms) however I need to write a specialization of the algorithm to act differently when the iterators point to complex values vs regular double values. Here is a basic example: #include <complex> #include <iostream> #include <vector> using namespace std; template <typename InputIt> void DoSomething(InputIt first, InputIt last) { cout << "Regular Double" << endl; for (; first != last; ++first) { cout << *first

Partial specialization failure with GCC

a 夏天 提交于 2021-01-28 06:03:45
问题 After answering this question I decided to dig deeper in the issue to find a minimal and replicable example with same error. Let assume I have the following primary template and I would like to specialize it to std::vector as follows: #include <vector> #include <iostream> template<typename T, typename T::value_type v> struct foo; template<typename T, T v> struct foo<std::vector<T>, v> { static constexpr T value = v; }; int main() { std::cout << foo<std::vector<int>, 32>::value << std::endl;

Partial specialization failure with GCC

泪湿孤枕 提交于 2021-01-28 05:56:35
问题 After answering this question I decided to dig deeper in the issue to find a minimal and replicable example with same error. Let assume I have the following primary template and I would like to specialize it to std::vector as follows: #include <vector> #include <iostream> template<typename T, typename T::value_type v> struct foo; template<typename T, T v> struct foo<std::vector<T>, v> { static constexpr T value = v; }; int main() { std::cout << foo<std::vector<int>, 32>::value << std::endl;

Template Specialization for Private Types

拈花ヽ惹草 提交于 2021-01-27 12:31:23
问题 I have a generic algorithm that needs to access its template type's traits. There is a trait class that can be specialized for providing these traits. When using this algorithm within my class, I'd like to use it with a private type defined within the class. However, specialization can only happen within namespace or global scope where my class is inaccessible. class A { struct Secret {}; }; template <typename T> struct Trait {}; // Inaccessible type ----vvvvvvvvv template <> struct Trait<A:

why template parameter which is explicitely given can not be “deduced”

时光总嘲笑我的痴心妄想 提交于 2021-01-05 08:55:42
问题 Coming from that question: Using enum values in combination with SFINAE I tried to implement: enum Specifier { One, Two, Three }; template <Specifier, typename UNUSED=void> struct Foo { void Bar(){ std::cout << "Bar default" << std::endl;} }; template <Specifier s , typename std::enable_if<s == Specifier::Two || s == Specifier::One, int>::type> struct Foo<s> { void Bar(){ std::cout << "Bar Two" << std::endl; } }; int main() { Foo< One >().Bar(); Foo< Two >().Bar(); } Fails with: > main.cpp

template class with a single method specialized in C++

只谈情不闲聊 提交于 2020-12-05 07:13:02
问题 I only have an hpp file for a school assignment in C++ (I am not allowed to add a cpp file, declaration and implementation should be both written in the file). I wrote this code inside it: template<class T> class Matrix { void foo() { //do something for a T variable. } }; I would like to add another foo method, but this foo() will be specialized for only an <int> . I have read in some places that I need to declare a new specialization class for this to work. But what I want is that the