关于 C++模板和运算符

发布于 2024-09-28 15:34:59 字数 973 浏览 8 评论 0原文

经过一番尝试和大量错误后,我发现它对于模板运算符来说并不是很有用。举个例子:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦过后 2024-10-05 15:34:59

这并不是具体的模板问题。这是一个语法问题。你所做的很奇怪,因为你只改变了返回类型。如果您更改了运算符参数,则不必显式提供模板的类型。由于您确实需要提供类型,因此需要显式调用运算符来应用参数,因为这是唯一的方法。

查看语法了解完整的详细信息。

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.

我乃一代侩神 2024-10-05 15:34:59

返回类型不能用于重载决策。你的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"];

星光不落少年眉 2024-10-05 15:34:59

是的,模板参数推导不适用于简单的函数返回类型。然而,模板运算符重载确实很有用,并且可以以一些完全巨大的方式应用。

下面是来自 boost 的代码示例: :凤凰

for_each(c.begin(), c.end(),
    if_(arg1 % 2 == 1)
    [
        cout << arg1 << ' '
    ]
);

正如您可能理解的那样,这是打印出容器中所有奇怪元素的简单方法。有点神奇。

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.

for_each(c.begin(), c.end(),
    if_(arg1 % 2 == 1)
    [
        cout << arg1 << ' '
    ]
);

As you might understand it is an easy way to print out all the odd elements in a container. Kinda magic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文