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

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

Typename and Struct definition in C++98

问题

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

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

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

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

#include <iostream>

template <typename T>
struct Pair
{
    T first = 0;
    T second = 0;
};

int main()
{
    Pair<int> p1 { 5, 6 };        
    std::cout << p1.first << ' ' << p1.second << '\n';

    p1 = { 2, 3 }; 
    std::cout << p1.first << ' ' << p1.second << '\n';

    return 0;
}

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

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

struct Pair
{
    T first;
    T second;
};

那么它将正确工作。

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

英文:

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:

#include &lt;iostream&gt;

template &lt;typename T&gt;
struct Pair
{
    T first = 0;
    T second = 0;
};

int main()
{
    Pair&lt;int&gt; p1 { 5, 6 };        
    std::cout &lt;&lt; p1.first &lt;&lt; &#39; &#39; &lt;&lt; p1.second &lt;&lt; &#39;\n&#39;;

    p1 = { 2, 3 }; 
    std::cout &lt;&lt; p1.first &lt;&lt; &#39; &#39; &lt;&lt; p1.second &lt;&lt; &#39;\n&#39;;


    return 0;
}

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:

struct Pair
{
    T first;
    T second;
};

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添加用户定义的构造函数:

template <typename T>
struct Pair
{
    T first;
    T second;

    Pair() : first(), second() {}
    Pair(T f, T s) : first(f), second(s) {}
};

int main()
{
    Pair<int> p1(5, 6);
    std::cout << p1.first << ' ' << p1.second << '\n';

    p1 = Pair<int>(2, 3);
    std::cout << p1.first << ' ' << p1.second << '\n';

    return 0;
}
英文:

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:

template &lt;typename T&gt;
struct Pair
{
    T first;
    T second;

    Pair() : first(), second() {}
    Pair(T f, T s) : first(f), second(s) {}
};

int main()
{
    Pair&lt;int&gt; p1(5, 6);
    std::cout &lt;&lt; p1.first &lt;&lt; &#39; &#39; &lt;&lt; p1.second &lt;&lt; &#39;\n&#39;;

    p1 = Pair&lt;int&gt;(2, 3);
    std::cout &lt;&lt; p1.first &lt;&lt; &#39; &#39; &lt;&lt; p1.second &lt;&lt; &#39;\n&#39;;


    return 0;
}

</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:

确定