LoginSignup
1
0

More than 5 years have passed since last update.

template関数を特定の型集合(浮動小数点型、整数型等)に特殊化する方法

Last updated at Posted at 2018-09-03

たまに使うのに調べてもパッと出てこなかったので自分用のメモ

test.cc
#include <iostream>
#include <type_traits>

using std::cout;
using std::endl;

template <class T,
         typename std::enable_if<std::is_floating_point<T>::value, T>::type* = nullptr>
void func(T num){
    cout << "This is a floating point number:" << num << endl;
}
template <class T,
         typename std::enable_if<std::is_integral<T>::value, T>::type* = nullptr>
void func(T num){
    cout << "This is an integer number:" << num << endl;
}
template <class T,
         typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value, T>::type* = nullptr>
void func(T num){
    cout << "This is neither integer nor floating number:" << num << endl;
}

int main(){
    func(123);
    func(123.);
    func(123.f);
    func("0x7B");
    return 0;
}
1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0