C++裏面的struct是可使用template的,然而http://c.biancheng.net/view/2235.html這裏居然說不能用。辣雞啊。html
#include <stdio.h> #include <iostream> using namespace std; template<class T> struct Box { private: T x, y; T w, h; public: Box<T>(T x_, T y_, T w_, T h_) { x = x_; y = y_; w = w_; h = h_; } }; int main(){ Box<float> box_float(1.0, 2.0, 3.0, 4.0); return 0; }
gcc編譯毫無壓力。ios
ref: https://stackoverflow.com/questions/2448242/struct-with-template-variables-in-cc++