template-function

Why doesn't my function skip trying to resolve to the incompatible template function, and default to resolving to the regular function? [duplicate]

删除回忆录丶 提交于 2019-12-05 18:15:37
This question already has an answer here: Why can't a template function resolve a pointer to a derived class to be a pointer to a base class 1 answer std::string nonSpecStr = "non specialized func"; std::string const nonTemplateStr = "non template func"; class Base {}; class Derived : public Base {}; template <typename T> std::string func(T * i_obj) { ( * i_obj) += 1; return nonSpecStr; } std::string func(Base * i_obj) { return nonTemplateStr; } void run() { // Function resolution order // 1. non-template functions // 2. specialized template functions // 3. template functions Base * base = new

inline template function?

假装没事ソ 提交于 2019-12-04 08:16:55
问题 Do I need inline template functions if they are included in several cpp files? Thanks. template<bool> inline QString GetText(); template<> inline QString GetText<true>() {return "true";} template<> inline QString GetText<false>() {return "false";} 回答1: You do, because those are full function specializations, and therefore subject to the one-definition rule just like normal functions. 回答2: Yes, you need the inline specifier there. The ODR (one-definition rule) states that there must be exactly

Why doesn't C++11 implicitly convert lambdas to std::function objects?

二次信任 提交于 2019-12-03 11:50:48
I implemented a generic event emitter class which allows code to register callbacks, and emit events with arguments. I used Boost.Any type erasure to store the callbacks so they can have arbitrary parameter signatures. It all works, but for some reason, lambdas being passed in must first be turned into std::function objects. Why doesn't the compiler infer that the lambda is the function type? Is it because of the way I use variadic templates? I use Clang (version string: Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn) ). Code: #include <functional> #include <iostream> #include

inline template function?

血红的双手。 提交于 2019-12-02 22:13:54
Do I need inline template functions if they are included in several cpp files? Thanks. template<bool> inline QString GetText(); template<> inline QString GetText<true>() {return "true";} template<> inline QString GetText<false>() {return "false";} You do, because those are full function specializations, and therefore subject to the one-definition rule just like normal functions. Yes, you need the inline specifier there. The ODR (one-definition rule) states that there must be exactly one definition of a variable, function, class, enum or template. Exceptions relevant for your question are

Specialization of template function after point of use will break the compilation

[亡魂溺海] 提交于 2019-12-01 18:41:46
Consider next example : #include <iostream> template< int a > void foo(); int main(int argn, char* argv[]) { foo<1>(); } template<> void foo<1>() { std::cout<<1<<std::endl; } The compilation fails with next error messages : rg.cpp:12: error: specialization of ‘void foo() [with int a = 1]’ after instantiation What paragraph in the standard explains this error? PS :I know that if I move the function definition in front of main will make the error go away. I think that's undefined behavior according to the standard. There are no restrictions on what a toolchain can do in cases of UB, generating a

Specialization of template function after point of use will break the compilation

时间秒杀一切 提交于 2019-12-01 18:06:04
问题 Consider next example : #include <iostream> template< int a > void foo(); int main(int argn, char* argv[]) { foo<1>(); } template<> void foo<1>() { std::cout<<1<<std::endl; } The compilation fails with next error messages : rg.cpp:12: error: specialization of ‘void foo() [with int a = 1]’ after instantiation What paragraph in the standard explains this error? PS :I know that if I move the function definition in front of main will make the error go away. 回答1: I think that's undefined behavior

Pull Apart Function Type With Specialized Function

允我心安 提交于 2019-12-01 10:42:41
The answer to this question picks apart a function type using a class template: template <typename T> struct function_args {}; template <typename R, typename... Args> struct function_args<R(Args...)> { using type = tuple<Args...>; }; template <typename T> using decltypeargs = typename function_args<T>::type; As I studied what was being done here I tried to rewrite function_args . I attempted to do this using a function so as to eliminate the need for the decltypeargs template. But found myself mired in improper syntax: template <typename T> tuple<> myTry(); template <typename Ret, typename...

Pull Apart Function Type With Specialized Function

假装没事ソ 提交于 2019-12-01 08:51:57
问题 The answer to this question picks apart a function type using a class template: template <typename T> struct function_args {}; template <typename R, typename... Args> struct function_args<R(Args...)> { using type = tuple<Args...>; }; template <typename T> using decltypeargs = typename function_args<T>::type; As I studied what was being done here I tried to rewrite function_args . I attempted to do this using a function so as to eliminate the need for the decltypeargs template. But found

Why can't a template function resolve a pointer to a derived class to be a pointer to a base class

徘徊边缘 提交于 2019-12-01 08:07:59
问题 Is the compiler unable, at compile-time, to take a pointer to a derived class and know that it has a base class? It seems like it can't, based on the following test. See my comment at the end for where the issue occurs. How can I get this to work? std::string nonSpecStr = "non specialized func"; std::string const specStr = "specialized func"; std::string const nonTemplateStr = "non template func"; class Base {}; class Derived : public Base {}; class OtherClass {}; template <typename T> std:

adapting a non-constexpr integral value to a non-type template parameter, and code bloat

限于喜欢 提交于 2019-12-01 05:53:56
Consider a function object F taking a constexpr size_t argument I struct F { template <size_t I> constexpr size_t operator()(size <I>) const { return I; } }; wrapped within a type size <I> , where (for brevity) template <size_t N> using size = std::integral_constant <size_t, N>; Of course, we could pass I directly but I want to emphasize that it is constexpr by using it as a template argument. Function F is dummy here but in reality it could do a variety of useful stuff like retrieving information from the I th element of a tuple. F is assumed to have the same return type regardless of I . I