LoginSignup
0
0

More than 5 years have passed since last update.

テンプレートパラメータによって引数の型を変えるテンプレート関数

Last updated at Posted at 2015-11-27

戻り値の形をテンプレートパラメータで変えるのはこないだやったので、今度は引数の形を変えてみる。

#include <iostream>
#include <string>
using namespace std;
enum class Type{
    MEMORY_SIZE,
    IP_ADDRESS
};

template <Type type>
struct Test_Traits{}; // empty class

template<>
struct Test_Traits<Type::MEMORY_SIZE>{
    typedef unsigned int type;
};


template<>
struct Test_Traits<Type::IP_ADDRESS>{
    typedef string type;
};

template <Type type>
void printParameter(typename Test_Traits<type>::type t){
        cout << t << endl;

}

int main(){
    printParameter<Type::MEMORY_SIZE>(300);
    printParameter<Type::IP_ADDRESS>("192.168.0.2");
    // printParameter<Type::MEMORY_SIZE>("192.168.0.2"); error
    return 0;
}

あ、普通にできるね。

0
0
0

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
0
0