Skip to content

Instantly share code, notes, and snippets.

@TheEyesightDim
Last active July 1, 2018 08:26
Show Gist options
  • Save TheEyesightDim/1d95725d30a04c53ef36b888aa8ce17c to your computer and use it in GitHub Desktop.
Save TheEyesightDim/1d95725d30a04c53ef36b888aa8ce17c to your computer and use it in GitHub Desktop.
Demonstrates the use of a variadic template function within a class template.
#ifndef HEADER_H
#define HEADER_H
#include <utility>
#include <algorithm>
#include <array>
template <typename T>
class MyClass
{
T m_heldType;
public:
MyClass(T t) : m_heldType(t) {}
//takes the maximum of a parameter pack of types T2, and binds it with member T to return a pair
template <typename T2,typename... Tn> //remember, Tn is a parameter pack, not a type
std::pair<T, T2> pairingFunc(const T2& arg, const Tn&... types) //This forces at least one arg
{
std::array<T2, sizeof...(types) + 1> types_arr = { arg, types... }; //this forces Tn to be of type T2
return std::make_pair( m_heldType, *std::max_element(types_arr.begin(), types_arr.end()) );
}
};
#endif
#include <iostream>
#include "Header.h"
int main()
{
MyClass<int> mc(512);
auto mypair = mc.pairingFunc<float>(7.61f, 3.14f, 6.66f);
std::cout << mypair.first << ' ' << mypair.second;
std::cin.ignore();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment