template-specialization

Specialize a void function template to a const char[N]

假如想象 提交于 2019-12-02 18:22:57
问题 I have a templated function which I want to specialize foo to const char[N] (hardcoded strings) template<typename T> const std::string foo() ; template<typename T,int N> const std::string foo<T[N]>() { return "T[N]"; } //this doesn't work for const char[12] template<> const std::string foo<const char*>() { return "Const char"; } //this doesn't work for const char[12] template<typename T> void someother function(T obj) { string s = foo<T>(); //I want to overload when T is const chat[N] }

Partially specialize methods of variadic template classes

夙愿已清 提交于 2019-12-02 14:14:56
问题 I want to write an unmarshaller to extract arguments stored in a msgpack array for individual arguments to a call of sigc::signal::emit(...) . I tried this: template<class... T> class MsgpackAdapter: public MsgpackAdapterBase { public: MsgpackAdapter (sigc::signal<void, T...> &sig) : MsgpackAdapterBase (), signal_ (sig) {} protected: virtual void do_emit (const msgpack::object_array &mp_args) override; private: sigc::signal<void, T...> signal_; }; template<class T1> void MsgpackAdapter<T1>:

Partially specialize methods of variadic template classes

自作多情 提交于 2019-12-02 08:28:09
I want to write an unmarshaller to extract arguments stored in a msgpack array for individual arguments to a call of sigc::signal::emit(...) . I tried this: template<class... T> class MsgpackAdapter: public MsgpackAdapterBase { public: MsgpackAdapter (sigc::signal<void, T...> &sig) : MsgpackAdapterBase (), signal_ (sig) {} protected: virtual void do_emit (const msgpack::object_array &mp_args) override; private: sigc::signal<void, T...> signal_; }; template<class T1> void MsgpackAdapter<T1>::do_emit (const msgpack::object_array &mp_args) { T1 a1; mp_args.ptr[0].convert (a1); signal_.emit (a1);

partial specialization with dependent name (typename)

眉间皱痕 提交于 2019-12-02 08:08:34
问题 I have the following simple strinToTypeImpl function which converts any kind of string into the template type. The problem I am concerned about is the fact that the compiler tells me for the partial specialization for typename MyMatrix<T>::Vector3 : template parameter T not used in partial specialization Can't I use dependent names in the specialization? namespace details { template<typename T> struct stringToTypeImpl{ bool operator()(T& t, const std::string& s) { std::istringstream iss(s);

Why full specialization of template function is not picked up from the .cpp file without declaration?

萝らか妹 提交于 2019-12-02 05:16:58
问题 Following code generate no compilation/linker error/warning: // A.h #include<iostream> struct A { template<typename T> static void foo (T t) { std::cout << "A::foo(T)\n"; } }; void other (); // main.cpp #include"A.h" int main () { A::foo(4.7); other(); } // other.cpp #include"A.h" template<> void A::foo (double d) { cout << "A::foo(double)\n"; } int other () { A::foo(4.7); } The output surprisingly is: A::foo(T) A::foo(double) Why compiler is not able to pick up the correct A::foo(double) in

partial specialization with dependent name (typename)

纵然是瞬间 提交于 2019-12-02 04:44:55
I have the following simple strinToTypeImpl function which converts any kind of string into the template type. The problem I am concerned about is the fact that the compiler tells me for the partial specialization for typename MyMatrix<T>::Vector3 : template parameter T not used in partial specialization Can't I use dependent names in the specialization? namespace details { template<typename T> struct stringToTypeImpl{ bool operator()(T& t, const std::string& s) { std::istringstream iss(s); return !(iss >> t).fail(); } }; template<typename T> struct stringToTypeImpl< typename MyMatrix<T>:

hide function template, declare specializations

邮差的信 提交于 2019-12-02 02:03:38
This is a followup to C++ templates: prevent instantiation of base template I use templates to achieve function overloading without the mess of implicit type conversions: declare the function template, define desired specializations (overloads). all is well except wrong code does not produce errors until the link phase: lib.hpp: template<class T> T f(T v); lib.cpp: #include "lib.hpp" template<> long f(long v) { return -v; } template<> bool f(bool v) { return !v; } main.cpp: #include <iostream> #include "lib.hpp" int main() { std::cout << f(123L) << ", " << f(true) << ", " << f(234) << "\n" ; }

Using template parameters as template parameters

好久不见. 提交于 2019-12-02 01:26:34
Why is the following code invalid? template <typename S, typename T> struct B{ void f(T t, S s) {t.f<S>(s); } }; gcc 4.3.4 complains that it "expected primary-expression before '>' token", i.e. that "S" wasn't a valid primary-expression. You need to specify that f is a template: void f(T t, S s) { t.template f<S>(s); } C++ doesn’t know this (at this point) since f ’s type depends on the type of the template parameter T . Furthermore, the following syntax would be ambiguous: does < mean the start of a template list or a less-than operator? To help C++ figure that out you need to specify that f

Why full specialization of template function is not picked up from the .cpp file without declaration?

旧街凉风 提交于 2019-12-02 00:19:56
Following code generate no compilation/linker error/warning: // A.h #include<iostream> struct A { template<typename T> static void foo (T t) { std::cout << "A::foo(T)\n"; } }; void other (); // main.cpp #include"A.h" int main () { A::foo(4.7); other(); } // other.cpp #include"A.h" template<> void A::foo (double d) { cout << "A::foo(double)\n"; } int other () { A::foo(4.7); } The output surprisingly is: A::foo(T) A::foo(double) Why compiler is not able to pick up the correct A::foo(double) in case of main.cpp ? Agree that, there is no issue as expected, if there is a declaration in A.h like

inheriting from class of specialized self?

谁说胖子不能爱 提交于 2019-12-01 21:18:50
问题 Is this valid C++? template<class category> class any_iterator : public any_iterator<void> { public: typedef any_iterator<void> any_iter_void; any_iterator() : any_iter_void() {} }; template<> class any_iterator<void> { public: typedef any_iterator<void> any_iter_void; any_iterator() {} void foo() {}; }; int main() { any_iterator<int> a; a.foo(); } MSVC10 accepts it with no errors/warnings on \WALL , but gcc-4.5.1 complains: prog.cpp:3:5: error: invalid use of incomplete type 'class any