C++98中的类型名和结构定义

huangapple go评论92阅读模式
英文:

Typename and Struct definition in C++98

问题

旧版C++标准中,使用structtypenames存在问题。我遇到了以下错误:

错误:没有匹配的函数调用

这是在使用C++98标准编译时出现的情况。但在使用C++11及更高版本时,它按照我的期望工作。

我定义了一个带有templatestruct,并在main()函数中使用一些值初始化了Pair。然后,我打印了结构的成员:

  1. #include <iostream>
  2. template <typename T>
  3. struct Pair
  4. {
  5. T first = 0;
  6. T second = 0;
  7. };
  8. int main()
  9. {
  10. Pair<int> p1 { 5, 6 };
  11. std::cout << p1.first << ' ' << p1.second << '\n';
  12. p1 = { 2, 3 };
  13. std::cout << p1.first << ' ' << p1.second << '\n';
  14. return 0;
  15. }

GDB编译器告诉我:
main.cpp: 在函数‘int main()’中: main.cpp:12:25: 错误:没有匹配的函数调用‘Pair::Pair()’ 12 | Pair<int> p1 { 5, 6 };

但是,如果我们将struct中成员的声明更改为以下内容:

  1. struct Pair
  2. {
  3. T first;
  4. T second;
  5. };

那么它将正确工作。

有人可以解释为什么会出现这种情况吗?

英文:

There is a problem with usage of typenames with struct in the old C++ standard. I have the following error:

> error: no matching function for call to

This is what I get when compiling with C++98 standard. With C++11 and newer, it works as I expect.

I defined a struct with a template and in the main() function I initialized the Pair from my templated struct with some values. Then I print the members of the struct:

  1. #include &lt;iostream&gt;
  2. template &lt;typename T&gt;
  3. struct Pair
  4. {
  5. T first = 0;
  6. T second = 0;
  7. };
  8. int main()
  9. {
  10. Pair&lt;int&gt; p1 { 5, 6 };
  11. std::cout &lt;&lt; p1.first &lt;&lt; &#39; &#39; &lt;&lt; p1.second &lt;&lt; &#39;\n&#39;;
  12. p1 = { 2, 3 };
  13. std::cout &lt;&lt; p1.first &lt;&lt; &#39; &#39; &lt;&lt; p1.second &lt;&lt; &#39;\n&#39;;
  14. return 0;
  15. }

GDB Compiler tells me:
main.cpp: In function ‘int main()’:
main.cpp:12:25: error: no matching function for call to ‘Pair::Pair()’
12 | Pair&lt;int&gt; p1 { 5, 6 };

But if we change declaration of members in the struct to this:

  1. struct Pair
  2. {
  3. T first;
  4. T second;
  5. };

It works correctly.

Can somebody explain why it works like this?

答案1

得分: 3

Default member initializers were a feature added to the language in C++11. That means that the = 0 part of your Pair member declarations is ill-formed before C++11.

默认成员初始化器是在C++11中添加的功能。这意味着在C++11之前,您的Pair成员声明中的= 0部分是不合法的。

Uniform initialization syntax was also added in C++11, which means that Pair<int> p1 { 5, 6 } and p1 = { 2, 3 } are also ill-formed prior to C++11.

统一初始化语法也是在C++11中添加的,这意味着在C++11之前,Pair<int> p1 { 5, 6 }p1 = { 2, 3 } 也是不合法的。

Changing the definition of Pair as you did fixes the first issue, but not the second. To get the same functionality with C++98 you will need to add user-defined constructors to Pair:

像您所做的那样更改Pair的定义会解决第一个问题,但不解决第二个问题。要在C++98中获得相同的功能,您需要向Pair添加用户定义的构造函数:

  1. template <typename T>
  2. struct Pair
  3. {
  4. T first;
  5. T second;
  6. Pair() : first(), second() {}
  7. Pair(T f, T s) : first(f), second(s) {}
  8. };
  9. int main()
  10. {
  11. Pair<int> p1(5, 6);
  12. std::cout << p1.first << ' ' << p1.second << '\n';
  13. p1 = Pair<int>(2, 3);
  14. std::cout << p1.first << ' ' << p1.second << '\n';
  15. return 0;
  16. }
英文:

Default member initializers were a feature added to the language in C++11. That means that the = 0 part of your Pair member declarations is ill-formed before C++11.

Uniform initialization syntax was also added in C++11, which means that Pair&lt;int&gt; p1 { 5, 6 } and p1 = { 2, 3 } are also ill-formed prior to C++11.

Changing the definition of Pair as you did fixes the first issue, but not the second. To get the same functionality with C++98 you will need to add user-defined constructors to Pair:

  1. template &lt;typename T&gt;
  2. struct Pair
  3. {
  4. T first;
  5. T second;
  6. Pair() : first(), second() {}
  7. Pair(T f, T s) : first(f), second(s) {}
  8. };
  9. int main()
  10. {
  11. Pair&lt;int&gt; p1(5, 6);
  12. std::cout &lt;&lt; p1.first &lt;&lt; &#39; &#39; &lt;&lt; p1.second &lt;&lt; &#39;\n&#39;;
  13. p1 = Pair&lt;int&gt;(2, 3);
  14. std::cout &lt;&lt; p1.first &lt;&lt; &#39; &#39; &lt;&lt; p1.second &lt;&lt; &#39;\n&#39;;
  15. return 0;
  16. }
  17. </details>

huangapple
  • 本文由 发表于 2023年7月10日 23:02:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定