template code declared outside of class doesn't compile

Advertisements I want to declare the constructor outside of the class but there is something wrong with the template code. template <size_t size = 1> class my_class { public: template <class int_t> constexpr my_class(int_t); }; template<class int_t, size_t size> constexpr my_class<size>::my_class(int_t num) { } but this works: template <size_t size = 1> class my_class {… Read More template code declared outside of class doesn't compile

How to determine function output type based on template parameters?

Advertisements The following construct is a minimal example of what I try to achieve: let the return type of a templated function depend on the input types. First I wrote (below a selection of a set that also specializes for std::complex<> variants): template<class U, class V> struct result_type { using type = U; }; template<>… Read More How to determine function output type based on template parameters?

How do I pass a 'general' priority_queue as function argument in c++

Advertisements In my main function, I have a bunch of priority_queues all of them have int in them, but some have less<int>, others have greater<int>: //std:: removed for clarity priority_queue<int, vector<int>, less<int>> pq1; priority_queue<int, vector<int>, greater<int>> pq2; I wish to make a function that accepts two priority_queue by reference and pops the first and puts… Read More How do I pass a 'general' priority_queue as function argument in c++

Correct template definition to wrap class member functions

Advertisements I looked up to this and this SO answers, but none solves my problem. Short description: the template function, where much more should happen than just that of this example, should be able to wrap any other function, also function members of a class. Minimal working example: #include <iostream> template <typename T, typename …ArgsT>… Read More Correct template definition to wrap class member functions

Syntactic restriction in function vs. function template when passing an address

Advertisements Let’s assume we want to print out the attribute of an arbitrary class through two different functions: One function takes a parameter const Widget&, the other one is a function template that takes a parameter of type const T&. We pass one argument of type Widget and Widget& respectively. The code can look like… Read More Syntactic restriction in function vs. function template when passing an address

Is template specilization in different source file undefined behavior?

Advertisements header.h: #include <iostream> template<typename T> class Test { public: __attribute__((noinline)) void fun(int a, int b) { std::cout << a + b << std::endl; } }; class SpecialClass {}; a.cpp: #include "header.h" int main() { Test<SpecialClass> test; test.fun(10, 8); return 0; } b.cpp: #include "header.h" template<> void Test<SpecialClass>::fun(int a, int b) { std::cout << a… Read More Is template specilization in different source file undefined behavior?

ambiguous call to function template (reverse)

Advertisements This code compiles with GCC, but gives ambiguous call to overloaded function for MSVC: https://godbolt.org/z/W89xn15d3 #include <string> template <typename Iter> void reverse(Iter begin, Iter end){ if (std::distance(begin, end) == 0) return; auto left = begin, right = std::prev(end); while (left < right) std::swap(*left++, *right–); } std::string reverseWordsInString(std::string str) { reverse(str.begin(), str.end()); // ambiguous call… Read More ambiguous call to function template (reverse)