关于 C++模板和运算符
经过一番尝试和大量错误后,我发现它对于模板运算符来说并不是很有用。举个例子:
class TemplateClass
{
//Generalized template
template<TType>
TType& operator[](const std::string& key)
{
return TType;
}
//Specialized template for int
template<>
int& operator[]<int>(const std::string& key)
{
return 5;
}
//Specialized template for char
template<>
char& operator[]<char>(const std::string& key)
{
return 'a';
}
}
int main()
{
TemplateClass test;
//this will not match the generalized template.
test["test without template will fail"];
//this is not how you call an operator with template.
test<char>["test with template will fail"];
//this works!
test.operator[]<int>["test will work like this"];
return 0;
}
所以用模板创建一个运算符是相当难看的(除非你很冗长,而且真的是谁?)这就是为什么我一直使用函数“get”来代替运算符。我的问题是为什么丑?为什么需要包含operator关键字。我的猜测是,这与转换运算符的一些后端魔法有关,不使用括号来接受参数,有什么办法可以解决这个问题吗?提前致谢。
After some trial and a lot of error, I have come to find out that it is not very useful to template operators. As an Example:
class TemplateClass
{
//Generalized template
template<TType>
TType& operator[](const std::string& key)
{
return TType;
}
//Specialized template for int
template<>
int& operator[]<int>(const std::string& key)
{
return 5;
}
//Specialized template for char
template<>
char& operator[]<char>(const std::string& key)
{
return 'a';
}
}
int main()
{
TemplateClass test;
//this will not match the generalized template.
test["test without template will fail"];
//this is not how you call an operator with template.
test<char>["test with template will fail"];
//this works!
test.operator[]<int>["test will work like this"];
return 0;
}
So to make an operator with a template is pretty ugly (unless you are into verbose, and really who is?) This is why I have been using a function "get" in lieu of operators. My question is why the ugliness? Why is it required to include the operator key word. My guess is that it has to do with some back end magic of transforming operators to not use parentheses to take arguments, and is there any way around this? Thanks in Advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这并不是具体的模板问题。这是一个语法问题。你所做的很奇怪,因为你只改变了返回类型。如果您更改了运算符参数,则不必显式提供模板的类型。由于您确实需要提供类型,因此需要显式调用运算符来应用参数,因为这是唯一的方法。
查看语法了解完整的详细信息。
This isn't specifically an issue of templates. It's a grammar issue. What you're doing is odd in that you're only changing the return type. Had you changed the operator parameters, you wouldn't have to explicitly provide the type for the template. Since you do need to provide type, you need to explicitly call the operator to apply the parameter because it's the only way.
Check out the grammar for complete details.
返回类型不能用于重载决策。你的operator[]声明的签名的唯一区别在于它们的返回类型,所以一旦你有两个,编译器就没有希望消除你的调用的歧义
test[“没有模板的测试将失败”] ;
Return types cannot be used for overloading resolution. The only difference in the signatures of your operator[] declarations is in their return types, so as soon as you had two of them, the compiler had no hope of disambiguating your call
test["test without template will fail"];
是的,模板参数推导不适用于简单的函数返回类型。然而,模板运算符重载确实很有用,并且可以以一些完全巨大的方式应用。
下面是来自 boost 的代码示例: :凤凰。
正如您可能理解的那样,这是打印出容器中所有奇怪元素的简单方法。有点神奇。
Yes, template argument deduction is not working with function return types to be simple. However template operator overloading is really useful and can be applied in some completely tremendous ways.
Below there is a code sample from boost::phoenix.
As you might understand it is an easy way to print out all the odd elements in a container. Kinda magic.