explicit-specialization

extending namespace std via partial template specialization

若如初见. 提交于 2020-01-24 12:13:09
问题 As far as I know, we are allowed (with some exceptions that I won't mention here) to "extend" namespace std by totally specializing a std template function such as std::swap , i.e. namespace std { template<> void swap<Foo>(Foo& lhs, Foo& rhs){...} } is perfectly valid. Since C++11, we can now partially specialize functions. I believe that we can then play the same game and extend std via partial specialization, like namespace std { template<typename T> void swap<Foo<T>>(Foo<T>& lhs, Foo<T>&

Is it valid to do explicit template specialisation with auto return 'type' in C++14?

倖福魔咒の 提交于 2020-01-23 07:26:13
问题 Previous question. I repeat the code from the previous question to make this question self-contained. The code below compiles and does not issue any warnings if it is compiled using gcc 4.8.3. with -std=c++1y . However, it does issue warnings if compiled with -std=c++0x flag. In the context of the previous question it was stated that the code does not compile using gcc 4.9.0. Unfortunately, at present, I do not fully understand how auto is implemented. Thus, I would appreciate if anyone could

How to symmetrically implement serialize and deserialize template functions in C++

冷暖自知 提交于 2020-01-07 04:20:22
问题 I want to write a serial of template functions to serialize and deserialize objects. I've finished the serialization part and everything works: #ifndef SERIALIZE_H #define SERIALIZE_H #include <string> #include <vector> #include <unordered_set> #include <unordered_map> #include <memory> inline std::string to_json(int value) { return std::to_string(value); } inline std::string to_json(long value) { return std::to_string(value); } inline std::string to_json(double value) { return std::to_string

How to provide a explicit specialization to only one method in a C++ template class?

偶尔善良 提交于 2020-01-03 11:38:00
问题 I have a template class that looks something like this: template<class T> class C { void A(); void B(); // Other stuff }; template<class T> void C<T>::A() { /* something */ } template<class T> void C<T>::B() { /* something */ } What I want is to provide an explicit specialization for only A while retaining the default for B and the "other stuff". What I have tried so far is class D { }; template<> void C<D>::A() { /*...*/ } // Gives a link error: multiple definition Every other variant I've

C++ explicit return type template specialisation

喜你入骨 提交于 2019-12-25 09:34:29
问题 This is a follow up on this (more general) question: previous question. A partial answer to the present question is given here: partial answer to the present question. I am interested in explicit specialisation of the return type based on the template argument. While the answer presented above provides a solution to the problem, I believe that there is a more elegant way of solving the problem using C++11/14 techniques: template<int N> auto getOutputPort2(); template<> auto getOutputPort2<0>(

SFINAE: detect existence of a template function that requires explicit specialization

﹥>﹥吖頭↗ 提交于 2019-12-22 04:17:11
问题 As a follow-up to my previous question, I am trying to detect the existence of a template function that requires explicit specialization. My current working code detects non-template functions (thanks to DyP's help), provided they take at least one parameter so that dependent name lookup can be used: // switch to 0 to test the other case #define ENABLE_FOO_BAR 1 namespace foo { #if ENABLE_FOO_BAR int bar(int); #endif } namespace feature_test { namespace detail { using namespace foo; template

Why won't “extern template” work with shared_ptr?

独自空忆成欢 提交于 2019-12-21 05:15:17
问题 I had the (seemingly) bright idea of using extern template class std::shared_ptr<SomeWidelyUsedClass> in stdafx.h immediately after #include <memory> in order to prevent std::shared_ptr<SomeWidelyUsedClass> from being redundantly instantiated in hundreds of files, figuring I could place template class std::shared_ptr<SomeWidelyUsedClass> in a single .cpp in order to force a single instantiation and hopefully save on compile/link time. However, examination of the resulting .cod and .obj files

Explicit specialization of member function template in source file

心不动则不痛 提交于 2019-12-18 13:20:11
问题 I have a class with a member template function: // writer.h class Writer { public: ... template <typename T, typename V> void addField(const std::string& name, V v) { // write something } }; And in Writer's source file, I added explicit specializations for some_type : // writer.cpp template <> void Writer::addField<some_type, int>(const std::string& name, int v) { // specific some_type writing logic } This works... sometimes. Even if I definitely make sure that I have the right types: writer

Way to set up class template with explicit instantiations

不打扰是莪最后的温柔 提交于 2019-12-11 02:36:08
问题 After asking this question and reading up a lot on templates, I am wondering whether the following setup for a class template makes sense. I have a class template called ResourceManager that will only be loading a few specific resources like ResourceManager<sf::Image> , ResourceManager<sf::Music> , etc. Obviously I define the class template in ResourceManager.h . However, since there are only a few explicit instantiations, would it be appropriate to do something like... // ResourceManager.cpp

How to do one explicit specialization for multiple types?

懵懂的女人 提交于 2019-12-10 19:32:31
问题 Considering a template function like below how is it possible to do explicitly specialize one version of function for multiple types: template <typename T> void doSomething(){ //whatever } The intention is to have one specialization instead of multiple following ones because //something is the same: void doSomething<int>(){ //something } void doSomething<float>(){ //something } void doSomething<double>(){ //something } any method to achieve one specialization? 回答1: You can't make template