function-templates

Parameterization and “function template partial specialization is not allowed”

丶灬走出姿态 提交于 2019-12-01 21:52:32
This is a continuation of What is the function parameter equivalent of constexpr? In the original question, we are trying to speed-up some code that performs shifts and rotates under Clang and VC++. Clang and VC++ does not optimize the code well because it treats the shift/rotate amount as variable (i.e., not constexpr ). When I attempt to parameterize the shift amount and the word size, it results in: $ g++ -std=c++11 -march=native test.cxx -o test.exe test.cxx:13:10: error: function template partial specialization is not allowed uint32_t LeftRotate<uint32_t, unsigned int>(uint32_t v) ^ ~~~~~

Templated Functions.. ERROR: template-id does not match any template declaration

和自甴很熟 提交于 2019-12-01 18:58:45
I have written a function template and an explicitly specialized templated function which simply takes in 3 arguments and calculates the biggest among them and prints it. The specialized function is causing an error,whereas the template works fine. But I want to work with char* type . This is the error I get=> error: template-id ‘Max<>’ for ‘void Max(char, char, char)’ does not match any template declaration Following is my code: template <typename T> void Max(T& a,T& b,T& c) { if(a > b && a >> c) { cout << "Max: " << a << endl; } else if(b > c && b > a) { cout << "Max: " << b << endl; } else

Why can't I use std::get<0> in std::transform?

倖福魔咒の 提交于 2019-12-01 16:11:04
问题 In trying to compile the following code which would copy a map s keys to a vector : map<string, string> mss; vector<string> vs; transform(mss.begin(), mss.end(), back_inserter(vs), get<0>); VS2013 can't distinguish which get is intended but this simpler usage works just fine: vs.push_back(get<0>(*mss.begin())); Specifying get<0, string, string> didn't help. What am I missing? 回答1: There are many overloads of std::get, where, in addition, each is a function template itself, therefore the

Why can't I use std::get<0> in std::transform?

故事扮演 提交于 2019-12-01 16:03:59
In trying to compile the following code which would copy a map s keys to a vector : map<string, string> mss; vector<string> vs; transform(mss.begin(), mss.end(), back_inserter(vs), get<0>); VS2013 can't distinguish which get is intended but this simpler usage works just fine: vs.push_back(get<0>(*mss.begin())); Specifying get<0, string, string> didn't help. What am I missing? There are many overloads of std::get , where, in addition, each is a function template itself, therefore the compiler can't tell which one you want at the call site where you request for the address of one of them. If you

partial specialization of function templates

只愿长相守 提交于 2019-11-30 09:44:33
In the below code snippet, template<typename T1> void func(T1& t) { cout << "all" << endl; } template<typename T2> void func(T2 &t) { cout << "float" << endl; } // I do not want this // template<> void func(float &t) int main() { int i; float f; func(i); // should print "all" func(f); // should print "float" return 0; } I would like to have the templates modified which by passing any type other than float will print "all" and passing float will print "float". I do not want template specialization, instead have partial specialization which will act accordingly based on input type. How should i

Why is `const T&` not sure to be const?

雨燕双飞 提交于 2019-11-29 22:47:22
template<typename T> void f(T a, const T& b) { ++a; // ok ++b; // also ok! } template<typename T> void g(T n) { f<T>(n, n); } int main() { int n{}; g<int&>(n); } Please note: b is of const T& and ++b is ok! Why is const T& not sure to be const? NathanOliver Welcome to const and reference collapsing . When you have const T& , the reference gets applied to T , and so does the const . You call g like g<int&>(n); so you have specified that T is a int& . When we apply a reference to an lvalue reference, the two references collapse to a single one, so int& & becomes just int& . Then we get to the

partial specialization of function templates

六月ゝ 毕业季﹏ 提交于 2019-11-29 14:02:30
问题 In the below code snippet, template<typename T1> void func(T1& t) { cout << "all" << endl; } template<typename T2> void func(T2 &t) { cout << "float" << endl; } // I do not want this // template<> void func(float &t) int main() { int i; float f; func(i); // should print "all" func(f); // should print "float" return 0; } I would like to have the templates modified which by passing any type other than float will print "all" and passing float will print "float". I do not want template

Why is `const T&` not sure to be const?

本小妞迷上赌 提交于 2019-11-28 19:30:15
问题 template<typename T> void f(T a, const T& b) { ++a; // ok ++b; // also ok! } template<typename T> void g(T n) { f<T>(n, n); } int main() { int n{}; g<int&>(n); } Please note: b is of const T& and ++b is ok! Why is const T& not sure to be const? 回答1: Welcome to const and reference collapsing. When you have const T& , the reference gets applied to T , and so does the const . You call g like g<int&>(n); so you have specified that T is a int& . When we apply a reference to an lvalue reference,

Passing (partially) templated template function as std::function(or function pointer)

可紊 提交于 2019-11-28 07:31:07
问题 #include <vector> #include <functional> template<class F> class Foo { public: template <class T> void std_function(std::function<F(std::vector<T>)> functor) { /* something */ } template <class T> void func_ptr(F (*funtor)(std::vector<T>)) { /* something else */ } }; template<class T, class F> F bar(std::vector<T>) { return F(); } int main() { Foo<double> test; std::function<double(std::vector<int>)> barz = bar<int, double>; test.std_function(bar<int, double>); //error 1 test.std_function(barz

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

孤街醉人 提交于 2019-11-28 00:51:49
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_result<MT::* ,A1>::type,boost::_mfi::dm<M,T>,_bi::list_av_1<A1>::type> // boost::bind(M T::* ,A1)' :