function-templates

Why do two functions have the same address?

青春壹個敷衍的年華 提交于 2019-11-27 09:43:22
Consider this function template: template<typename T> unsigned long f(void *) { return 0;} Now, I print the addresses of f<A> and f<B> as: std::cout << (void*)f<A> << std::endl; std::cout << (void*)f<B> << std::endl; Why do they print the same address if compiled in MSVS10? Are they not two different functions and therefore should print different addresses? Updated: I realized that on ideone, it prints the different address. MSVS10 optimizes the code, as the function doesn't depend on T in any way, so it produces same function. @Mark's answer and comments on this are valuable. :-) Mark Ransom

How to pass a template function in a template argument list

情到浓时终转凉″ 提交于 2019-11-27 08:50:55
Suppose I have a template function: template<typename T> T produce_5_function() { return T(5); } How can I pass this entire template to another template ? If produce_5_function was a functor, there would be no problem: template<typename T> struct produce_5_functor { T operator()() const { return T(5); } }; template<template<typename T>class F> struct client_template { int operator()() const { return F<int>()(); } }; int five = client_template< produce_5_functor >()(); but I want to be able to do this with a raw function template: template<??? F> struct client_template { int operator()() const

Will specialization of function templates in std for program-defined types no longer be allowed in C++20?

安稳与你 提交于 2019-11-27 02:33:33
问题 Quote from cppreference.com: Adding template specializations It is allowed to add template specializations for any standard library |class (since C++20)| template to the namespace std only if the declaration depends on at least one program-defined type and the specialization satisfies all requirements for the original template, except where such specializations are prohibited. Does it mean, that starting from C++20, adding specializations of function templates to the std namespace for user

Can I use (boost) bind with a function template?

别说谁变了你拦得住时间么 提交于 2019-11-26 21:47:57
问题 Is it possible to bind arguments to a function template with (boost) bind? // Define a template function (just a silly example) template<typename ARG1, typename ARG2> ARG1 FCall2Templ(ARG1 arg1, ARG2 arg2) { return arg1 + arg2; } // try to bind this template function (and call it) ... boost::bind(FCall2Templ<int, int>, 42, 56)(); // This works boost::bind(FCall2Templ, 42, 56)(); // This emits 5 pages of error messages on VS2005 // beginning with: error C2780: // 'boost::_bi::bind_t<_bi::dm

Partial ordering with function template having undeduced context

无人久伴 提交于 2019-11-26 19:52:08
问题 While reading another question, i came to a problem with partial ordering, which i cut down to the following test-case template<typename T> struct Const { typedef void type; }; template<typename T> void f(T, typename Const<T>::type*) { cout << "Const"; } // T1 template<typename T> void f(T, void*) { cout << "void*"; } // T2 int main() { // GCC chokes on f(0, 0) (not being able to match against T1) void *p = 0; f(0, p); } For both function templates, the function type of the specialization

Variadic function template with pack expansion not in last parameter

落花浮王杯 提交于 2019-11-26 17:58:53
I am wondering why the following code doesn't compile: struct S { template <typename... T> S(T..., int); }; S c{0, 0}; This code fails to compile with both clang and GCC 4.8. Here is the error with clang: test.cpp:7:3: error: no matching constructor for initialization of 'S' S c{0, 0}; ^~~~~~~ test.cpp:4:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided S(T..., int); ^ It seems to me that this should work, and T should be deduced to be a pack of length 1. If the standards forbids doing things like this, does anyone know why? Because when a function parameter

Why do two functions have the same address?

心已入冬 提交于 2019-11-26 17:52:19
问题 Consider this function template: template<typename T> unsigned long f(void *) { return 0;} Now, I print the addresses of f<A> and f<B> as: std::cout << (void*)f<A> << std::endl; std::cout << (void*)f<B> << std::endl; Why do they print the same address if compiled in MSVS10? Are they not two different functions and therefore should print different addresses? Updated: I realized that on ideone, it prints the different address. MSVS10 optimizes the code, as the function doesn't depend on T in

Why function template cannot be partially specialized?

旧街凉风 提交于 2019-11-26 15:58:58
I know the language specification forbids partial specialization of function template. I would like to know the rationale why it forbids it? Are they not useful? template<typename T, typename U> void f() {} //allowed! template<> void f<int, char>() {} //allowed! template<typename T> void f<char, T>() {} //not allowed! template<typename T> void f<T, int>() {} //not allowed! Cheers and hth. - Alf AFAIK that's changed in C++0x. I guess it was just an oversight (considering that you can always get the partial specialization effect with more verbose code, by placing the function as a static member

How to pass a template function in a template argument list

本秂侑毒 提交于 2019-11-26 14:12:01
问题 Suppose I have a template function: template<typename T> T produce_5_function() { return T(5); } How can I pass this entire template to another template ? If produce_5_function was a functor, there would be no problem: template<typename T> struct produce_5_functor { T operator()() const { return T(5); } }; template<template<typename T>class F> struct client_template { int operator()() const { return F<int>()(); } }; int five = client_template< produce_5_functor >()(); but I want to be able to

Can a class member function template be virtual?

可紊 提交于 2019-11-25 23:59:35
问题 I have heard that C++ class member function templates can\'t be virtual. Is this true? If they can be virtual, what is an example of a scenario in which one would use such a function? 回答1: Templates are all about the compiler generating code at compile-time . Virtual functions are all about the run-time system figuring out which function to call at run-time . Once the run-time system figured out it would need to call a templatized virtual function, compilation is all done and the compiler