C++使用进行模板特化没有获取 int
我有以下代码:
template <typename T> LuaCall& operator>>(T) { BOOST_STATIC_ASSERT(sizeof(T) == 0); }
template <> LuaCall& operator>><int&>(int& val) { mResults.push_back(std::make_pair(LUA_RESULT_INTEGER, (void *)&val)); return *this; }
template <> LuaCall& operator>><float&>(float& val) { mResults.push_back(std::make_pair(LUA_RESULT_FLOAT, (void *)&val)); return *this; }
template <> LuaCall& operator>><double&>(double& val) { mResults.push_back(std::make_pair(LUA_RESULT_DOUBLE, (void *)&val)); return *this; }
template <> LuaCall& operator>><bool&>(bool& val) { mResults.push_back(std::make_pair(LUA_RESULT_BOOLEAN, (void *)&val)); return *this; }
template <> LuaCall& operator>><std::string&>(std::string& val) { mResults.push_back(std::make_pair(LUA_RESULT_STRING, (void *)&val)); return *this; }
template <> LuaCall& operator>><LuaNilStruct>(LuaNilStruct) { mResults.push_back(std::make_pair(LUA_RESULT_NIL, (void *)NULL)); return *this; }
然后:
int abc;
LuaCall(l, "test") % "test" % 5 % LuaNil % 2.333 >> abc;
我希望它的工作方式有点像 cin >>确实如此,即需要将lua函数的返回值写入abc。所以我需要它的地址..但它默认在默认模板上。我做错了什么?肯定有一种方法可以做到这一点,因为 cin 正是这样做的。
谢谢!
请注意将 % 更改为 >> 的人:我把它改回来了,因为它本来就是这样的 :D 代码调用 Lua 函数 test("test", 5, nil, 2.333) 并将其返回值保存到abc。 % 代表函数的参数,>> 代表返回值。
template <typename T>
LuaCall& operator%(T val) {
mLua->Push(val);
++mArguments;
return *this;
}
I have the following code:
template <typename T> LuaCall& operator>>(T) { BOOST_STATIC_ASSERT(sizeof(T) == 0); }
template <> LuaCall& operator>><int&>(int& val) { mResults.push_back(std::make_pair(LUA_RESULT_INTEGER, (void *)&val)); return *this; }
template <> LuaCall& operator>><float&>(float& val) { mResults.push_back(std::make_pair(LUA_RESULT_FLOAT, (void *)&val)); return *this; }
template <> LuaCall& operator>><double&>(double& val) { mResults.push_back(std::make_pair(LUA_RESULT_DOUBLE, (void *)&val)); return *this; }
template <> LuaCall& operator>><bool&>(bool& val) { mResults.push_back(std::make_pair(LUA_RESULT_BOOLEAN, (void *)&val)); return *this; }
template <> LuaCall& operator>><std::string&>(std::string& val) { mResults.push_back(std::make_pair(LUA_RESULT_STRING, (void *)&val)); return *this; }
template <> LuaCall& operator>><LuaNilStruct>(LuaNilStruct) { mResults.push_back(std::make_pair(LUA_RESULT_NIL, (void *)NULL)); return *this; }
And then:
int abc;
LuaCall(l, "test") % "test" % 5 % LuaNil % 2.333 >> abc;
I want it to work kinda like cin >> does, ie it needs to write to abc the return value of the lua function. So I need its address.. but it defaults on the default template. What am I doing wrong? There is surely a way to do this since cin does exactly that.
Thanks!
Note to whoever changed the %'s to >>: I changed it back since it's the way it is :D The code calls the Lua function test("test", 5, nil, 2.333) and saves its return value to abc. %'s are for the parameters of the functions, >>'s are for the return value(s).
template <typename T>
LuaCall& operator%(T val) {
mLua->Push(val);
++mArguments;
return *this;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您编写了运算符>>作为一元运算符,但它是一个二元运算符。
LeftHandSide>>右手边
。形式
std::cout <"Hello" << " world";
因此是(operator<<(operator<<(std::cout, "Hello"), " world)
; - 第一个< <
返回 std::cout 用作第二个<<
的左侧。真正的问题是在您的代码中,查找发生如下:
在步骤 3 中,
T==int< 没有特化。 /code> 因此,您可能希望使用重载而不是特化,因此您甚至不需要进行模板参数推导。
You'v written operator>> as a unary operator, but it's a binary operator.
LeftHandSide >> RightHandSide
.The form
std::cout <"Hello" << " world";
therefore is(operator<<(operator<<(std::cout, "Hello"), " world)
; - the first<<
returns std::cout for use as the left-hand side of the second<<
.The real problem is that in your code, lookup happens as follows:
In step 2, T==int. In step 3, there are no specializations for
T==int
so the base template is chosen and instantiated. You might want to use an overload instead of a specialization. The overload would be a better match in step 1 so you don't even get to the point of template argument deduction您不能在需要引用的地方使用常量值(
"test"、5 或 2.333
)。当您需要此行为时,请将operator>>
的模板和参数类型更改为(int、float
等)。You cannot use constant values (
"test" , 5 or 2.333
) where references are expected. Change the template and parameter type of youroperator>>
to (int, float
etc.) when you want this behaviour.首先,
operator>>
是一个二元运算符。这意味着它必须有两个参数。第一个参数必须是由于该运算符返回 LuaCall& 的类型,我想这就是该调用的结果(以及左参数)。
从该
mResult
我认为您尝试以类成员的身份执行该运算符。但我不认为你可以专门化成员模板。最后,我认为您不应该专门研究引用类型。这
应该做
For one,
operator>>
is a binary operator. That means it has to take two arguments. The first argument has to be whatever is the type ofSince that operator returns
LuaCall&
, I suppose that's what the result of that call (and thus left argument) is.From that
mResult
I suppose that you attempt to do that operator as a class member. But I don't think you can specialize member templates.Finally, I don't think you should specialize for reference types. This
should do