RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1575342
Accepted
Halturin Evgeniy
Halturin Evgeniy
Asked:2024-04-09 14:24:50 +0000 UTC2024-04-09 14:24:50 +0000 UTC 2024-04-09 14:24:50 +0000 UTC

构造函数和类方法中类型特征的不同行为

  • 772

这是我的类,其中构造函数调用方法作为初始值设定项。构造函数和这些方法的签名是相同的(唯一的区别是有些是构造函数,而另一些是返回某些内容的方法)

#include <iostream>
#include <vector>
#include <type_traits>
#include <concepts>

// TMyClass ********************************************************************************
template <size_t Size>
class TMyClass {
public:

    using TData = std::vector<size_t>;

private:

    TData data;

protected:

    template <std::integral size_type>
    TData InitData(const size_type(&arr)[Size]) {
        std::vector<size_t> tmp(Size);
        for (size_t i = 0; i < Size; ++i) {
            tmp[i] = arr[i];
        }
        return tmp;
    }

    template <std::integral size_type>
    TData InitData(const size_type* arr, size_t size = Size) {
            if (size != Size) throw;
        std::vector<size_t> tmp(Size);
        for (size_t i = 0; i < Size; ++i) {
            tmp[i] = arr[i];
        }
        return tmp;
    }

public:

    template <std::integral size_type>
    TMyClass(const size_type(&arr)[Size])
        : data(InitData(arr)) // <------------------ ERROR
    {}

    template <std::integral size_type>
    TMyClass(const size_type* arr, size_t size = Size)
        : data(InitData(arr, size))
    {}

};

// M A I N ********************************************************************************
int main()
{
    int arr[3] = { 3, 4, 5 };
    TMyClass<3> obj1(arr);

    std::vector<int> vec = { 1, 2, 3 };
    TMyClass<3> obj2(vec.data()); // <------------------ ?????


    std::cout << "\n\n  - the end -\n";
    std::cout << "press [Enter]...";
    std::cin.get();
}

结果,我收到错误(代码中的行标记为“<-------------------- ERROR”):

TMyClass<3>::InitData:对重载函数的不明确调用

看起来一切都是合乎逻辑的,纠正是

template <std::integral size_type>
TData InitData(const size_type* arr, size_t size) {
// ...
}

(删除参数的默认值size)解决了问题。

但!那么为什么线上不会出现这个错误

TMyClass<3> obj2(vec.data()); // <------------------ ?????

毕竟在构造函数中

template <std::integral size_type>
TMyClass(const size_type* arr, size_t size = Size)
    : data(InitData(arr, size))
{}

该参数的默认值size仍然存在。

PS:问题不在于课程的逻辑:什么是正确/不正确?问题是:为什么在相同的(在我看来)情况下会有不同的行为?

PPS:使用标准 type_traits

模板 <类型名称 size_type, std::enable_if_t< std::is_integral_v<size_type>, bool > = true>

给出相同的结果

PPS:在 Visual Studio 和 Qt 上测试

c++
  • 2 2 个回答
  • 47 Views

2 个回答

  • Voted
  1. Best Answer
    isnullxbh
    2024-04-09T19:32:27Z2024-04-09T19:32:27Z

    但!那么为什么线上不会出现这个错误

    因为存在隐式数组到指针的转换,但反之则不然:)

    int arr[3];
    int (&r1)[3] = arr; // Ok
    int*  r2     = arr; // Ok
    
    int* ptr;
    int (&r3)[3] = ptr; // Ошибка
    int*  r4     = ptr; // Ок
    
    • 1
  2. Halturin Evgeniy
    2024-04-09T20:04:44Z2024-04-09T20:04:44Z

    看来我通过@Harry 和@isnullxbh 找到了答案。确实,我的猜测是:

    问题是:为什么在相同的(在我看来)情况下会有不同的行为?

    不是真的。我没有密切关注类型转换。

    代码的行为完全符合我的预期:

    #include <iostream>
    #include <vector>
    #include <concepts>
    
    template <typename T>
    concept integral_pointer = std::is_pointer_v<T> && std::integral<typename std::remove_pointer<T>::type>;
    
    // TMyClass ********************************************************************************
    template <size_t Size>
    class TMyClass {
    public:
    
        using TData = std::vector<size_t>;
    
    private:
    
        TData data;
    
    protected:
    
        template <std::integral size_type>
        TData InitData(const size_type(&arr)[Size]) {
            TData tmp(Size);
            for (size_t i = 0; i < Size; ++i) {
                tmp[i] = arr[i];
            }
            return tmp;
        }
    
        template <integral_pointer size_type_ptr>
        TData InitData(size_type_ptr arr, size_t size = Size) {
            if (size != Size) throw;
            TData tmp(Size);
            for (size_t i = 0; i < Size; ++i) {
                tmp[i] = arr[i];
            }
            return tmp;
        }
    
    public:
    
        template <std::integral size_type>
        TMyClass(const size_type(&arr)[Size])
            : data(InitData(arr))
        {}
        
        template <integral_pointer size_type_ptr>
        TMyClass(size_type_ptr arr, size_t size = Size)
            : data(InitData(arr, size))
        {}
    
    };
    
    // M A I N ********************************************************************************
    int main()
    {
        int arr[3] = { 3, 4, 5 };
        TMyClass<3> obj0(arr);
    
        const int (&ref_arr)[3] = arr;
        TMyClass<3> obj1(ref_arr);
    
        int* const ptr = new int[3]{ 1, 2, 3 };
        TMyClass<3> obj2(ptr);
    
        int const* const& ref_ptr = ptr;
        TMyClass<3> obj3(ref_ptr);
    
        delete[] ptr;
    
        std::cout << "\n\n  - the end -\n";
        std::cout << "press [Enter]...";
        std::cin.get();
    }
    
    • 1

相关问题

  • 编译器和模板处理

  • 指针。找到最小数量

  • C++,关于枚举类对象初始化的问题

  • 函数中的二维数组

  • 无法使用默认构造函数创建类对象

  • C++ 和循环依赖

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5