template-specialization

Type_traits *_v variable template utility order fails to compile

老子叫甜甜 提交于 2019-12-10 02:58:57
问题 Having seen this answer, I tried to come up with a variable template utility to the code from it: template <class T, template <class...> class Template> struct is_specialization : std::false_type {}; template <template <class...> class Template, class... Args> struct is_specialization<Template<Args...>, Template> : std::true_type {}; And implement it like so: template <template <class...> class Template, class... Args> constexpr bool is_specialization_v = is_specialization<Template<Args...>,

SFINAE template specialization precedence

五迷三道 提交于 2019-12-10 01:33:49
问题 #include <iostream> #include <array> #include <vector> template <typename T, typename SFINAE=void> struct trait; template <typename T> struct trait<T, decltype( std::declval<const T&>().begin(), std::declval<const T&>().end(), void() )> { static const char* name() { return "Container"; } }; template <typename T, std::size_t N> struct trait<std::array<T,N>> { static const char* name() { return "std::array"; } }; int main(int argc, char* argv[]) { std::cout << trait<std::vector<int>>::name() <<

Specialize Many Templates for a Set of Types

丶灬走出姿态 提交于 2019-12-10 00:01:01
问题 How to specialize many template for all kinds of scalar values? (such as int , float , size_t , uint32_t , and types defined in the stdint header)? Can I avoid specializing each template for each of the types? I don't want to use boost or other non-standard libraries if possible. There are some solutions at template specialization for a set of types: Replace each template with multiple functions. One function for each scalar type. (But there are many templates. That would mean writing many

Make C++ fail compilation on specific instantiation of template function

我们两清 提交于 2019-12-09 17:53:56
问题 I'm working on a project which has an template function as so: template <class T> T foo<T>(T val) { return someFunc(val); } template <> bool foo<bool>(bool val) { return otherFunc(val); }; Now, I have a class Bar , which I don't want to accept as input. In fact, I want it to generate an easy to spot compile error. The problem is that if I do this: template <> Bar foo<Bar>(Bar val) { static_assert(false,"uh oh..."); } It fails on every compile. I found https://stackoverflow.com/a/3926854

c++ template specialization - linker error multiple definitions

两盒软妹~` 提交于 2019-12-09 14:44:04
问题 My third question here today ;-), but I am really new to c++ template programming and operator overloading. I am trying the following: terminallog.hh //snipped code class Terminallog { public: Terminallog(); Terminallog(int); virtual ~Terminallog(); template <class T> Terminallog & operator<<(const T &v); template <class T> Terminallog & operator<<(const std::vector<T> &v); template <class T> Terminallog & operator<<(const T v[]); Terminallog & operator<<(const char v[]); //snipped code }; /

Why is a partial class template specialization on a matching template class ambiguous with another partial specialization without the template match?

蓝咒 提交于 2019-12-09 12:46:18
问题 The question may be too hard to describe in on sentence in the title, but here is a minimal example: #include <iostream> #include <type_traits> template <class T, class U, class Enabler> struct my_trait : std::false_type {}; template <class T, class U> struct my_trait<T, U, std::enable_if_t<std::is_same<T, U>::value>> : std::true_type {}; template <class T> class temped {}; template <class T> struct my_trait<temped<T>, temped<T>, void> : std::false_type {}; template <class T, class U> using

What is the best way to create a specialization-only function template?

白昼怎懂夜的黑 提交于 2019-12-09 09:31:51
问题 Is there a better way to do the following? #include <iostream> template <typename T> T Bar(); template <> int Bar<int>() { return 3; } // Potentially other specialisations int main() { std::cout << Bar<int>() << std::endl; // This should work std::cout << Bar<float>() << std::endl; // This should fail } The problem with this solution is that it fails at (understandably) link time with "undefined reference to float Bar<float>() " or the like. This can be confusing for other developers as they

How to specialize Iterator by its value type, in C++?

有些话、适合烂在心里 提交于 2019-12-09 08:20:24
问题 Is it possible to specialize an Iterator template parameter by its value_type ? I have a function with the following prototype. template<typename InputIterator> void f(InputIterator first, InputIterator last); And I want to handle specially if InputIterator::value_type is SomeSpecificType. 回答1: You can use some intermediate structs to get the partial template specialisation that you need. Something like this should do the trick template<typename T, typename V> struct f_impl { static void f( T

Declaration of template class member specialization

柔情痞子 提交于 2019-12-08 14:58:16
问题 When I specialize a (static) member function/constant in a template class, I'm confused as to where the declaration is meant to go. Here's an example of what I what to do - yoinked directly from IBM's reference on template specialization: ===IBM Member Specialization Example=== template<class T> class X { public: static T v; static void f(T); }; template<class T> T X<T>::v = 0; template<class T> void X<T>::f(T arg) { v = arg; } template<> char* X<char*>::v = "Hello"; template<> void X<float>:

Specializations only for C++ template function with enum non-type template parameter

旧时模样 提交于 2019-12-08 14:33:26
This question is related to this one except that rather than dealing with typename template parameters, I am trying to use an enum non-type template parameter. Is it possible to have a templated (class member function) with only specializations, no general (working) definition in the case of non-type template parameter? I was able to get one version working, by declaration in the class body and providing specializations only, but any misuse calling with a non-defined template parameter doesn't produce an error until linking. What's worse is the missing symbol cryptically refers to the enum's