我迷失了模板。boost :: tuple和generic結構體的不同模板專業化
我有這樣的代碼在頭:
void push(lua_State *, bool);
void push(lua_State *, int);
void push(lua_State *, long);
void push(lua_State *, unsigned long);
void push(lua_State *, lua_Number);
void push(lua_State *, lua_CFunction);
void push(lua_State *, const char *);
void push(lua_State *, const char *, std::size_t);
void push(lua_State *, const std::string &);
template<typename T>
void push(lua_State * L, const std::vector<T> & value)
{
lua_newtable(L);
std::size_t size = value.size();
for(unsigned int i = 0; i < size; i++)
{
lua_pushinteger(L, i + 1);
push(L, value[i]);
lua_settable(L, -3);
}
}
inline void push(lua_State *, boost::tuples::null_type){}
inline void push(lua_State *, boost::tuple<>){}
template<typename HT, typename TT>
void push(lua_State * L, const boost::tuples::cons<HT, TT> & value){
push(L, value.get_head());
push(L, value.get_tail());
}
//Declared but not defined. *Intentional* compile-time error if trying to push a Luaproxy (undefined reference). Use Lua stack reference instead
template<typename T>
void push(lua_State* l, Luaproxy<T>& value);
template<typename T> //most generic version
void push(lua_State* l, T& value){
Luaproxy<T>::new_c(l, value, false);
}
我希望看到boost::tuples::cons
版push()
調用時爲第二個參數傳遞的boost::make_tuple(...)
結果。但是我在鏈接時遇到錯誤,表明這些調用已解析爲push
的最普通版本,即push(lua_State* l, T& value)
。
最終的結果是,我一定能夠做到
mystruct example;
push(l, boost::make_tuple(3, 4, example))
而這又必須
void push(lua_State * L, const boost::tuples::cons<HT, TT> & value) //tuple contains int, int, mystruct
void push(lua_State *, int)
void push(lua_State * L, const boost::tuples::cons<HT, TT> & value) //my tuple contains int mystruct
void push(lua_State *, int)
void push(lua_State * L, const boost::tuples::cons<HT, TT> & value) //my tuple contains mystruct
void push(lua_State* l, T& value)
調用如果我刪除我的push()
功能最普遍的形式,其餘我的應用程序的代碼,它調用push()
與作爲參數的一個typle正確解析到push(lua_State * L, const boost::tuples::cons<HT, TT> & value)
版本的函數。所以,出於某種原因,最通用的版本具有更高的優先級,這就是我不想要的。
編輯: 我試圖按照Anycorn的建議,但我不是很擅長這一點。我要離開boost::tuples::cons
版本是,我想禁用如果將參數從boost::tuples::cons
得出的通用版本:
template<typename T, typename HT, typename TT>
void push(lua_State* l, T& value, typename boost::disable_if<boost::is_base_of< boost::tuples::cons<HT, TT>, T>, T>::type* =0){
Luaproxy<T>::new_c(l, value, false);
}
而且它仍在考慮利弊鍵入push(l, boost::make_tuple(mystruct))
[切] /home/pisto/sorgenti/hopmodv4/src/hopmod/lua/push_function.hpp:48:6:note:template void lua :: push(lua_State *,lua :: Luaproxy &) /home/pisto/sorgenti /hopmodv4/src/hopmod/lua/push_function.hpp:51:6:note:template void lua :: push(lua_State *,const T &, typename boost :: disable_if,T>,T> :: type *) /home/pisto/sorgenti/hopmodv4/src/hopmod/lua/push_function.hpp:56:6:note:template void lua :: push(lua_State *,常量的boost ::元組::利弊&)
好像你正在嘗試做部分專業化的功能 - 你不能 – Anycorn 2011-12-31 00:42:26