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

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 into… Read More How do I pass a 'general' priority_queue as function argument in c++

Correct template definition to wrap class member functions

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> T… Read More Correct template definition to wrap class member functions

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

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 the… Read More Syntactic restriction in function vs. function template when passing an address

Is template specilization in different source file undefined behavior?

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)

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)

Use of std::size_t in array function template header (C++)

In this code: #include <cstddef> #include <array> #include <iostream> // here template <typename T, std::size_t size> void printArray(const std::array<T, size>& myArray) { for (auto element : myArray) std::cout << element << ‘ ‘; std::cout << ‘\n’; } int main() { std::array myArray1{ 9.0, 7.2, 5.4, 3.6, 1.8 }; printArray(myArray1); // shouldn’t this line be printArray<double,… Read More Use of std::size_t in array function template header (C++)

C++ Instantiate template template class

I am trying to implement my own container (in my demonstration called Manager), which stores another templated container (e.g Node<T>). Therefore I am trying to instantiate an object like Mgr<Node<T>>. After reading through all of the template template-articles on here, I came to the following demonstration code. #include <iostream> template<typename T> struct Node { T… Read More C++ Instantiate template template class

How can I pass a function template as (template) argument to a function template?

Currently, I have a code like #include <cstdint> #include <iostream> template<std::uint32_t idx> void f() { std::cout << "f(" << idx << ")\n"; } template<std::uint32_t n> void loop1() { f<n>(); if constexpr (n > 0) { loop1<n – 1>(); } } template<std::uint32_t idx> void g() { std::cout << "g(" << idx << ")\n"; } template<std::uint32_t n>… Read More How can I pass a function template as (template) argument to a function template?