c++ - Constexpr 值作为指针类型的非类型模板参数

标签 c++ templates c++17 constexpr

以下code

#include <iostream>
#include <initializer_list>

using namespace std;


constexpr initializer_list<int> list = {1, 2, 3};

template<const int* begin, const int* end>
bool contains(int v)
{
    if constexpr(begin != end)
    {
        if (*begin = v)
            return true;
        else
            return contains<next(begin), end>(v);
    }
    return false;
}


int main()
{
    cout << contains<list.begin(), list.end()>(2);
    return 0;
}

产生一些非常奇怪的错误信息:

main.cpp: In function 'int main()':
main.cpp:25:49: error: no matching function for call to 'contains<list.std::initializer_list<int>::begin(), list.std::initializer_list<int>::end()>(int)'
     cout << contains<list.begin(), list.end()>(2);
                                                 ^
main.cpp:10:6: note: candidate: 'template<const int* begin, const int* end> bool contains(int)'
 bool contains(int v)
      ^~~~~~~~
main.cpp:10:6: note:   template argument deduction/substitution failed:
main.cpp:25:49: error: the address of '._86' is not a valid template argument
     cout << contains<list.begin(), list.end()>(2);
                                                 ^
main.cpp:25:49: error: 'list.std::initializer_list<int>::end()' is not a valid template argument for 'const int*' because it is not the address of a variable

我相信这可以编译,因为编译器在编译时拥有所有信息:list 是 constexpr,它的 begin 也是,endnext。那么是什么阻止了它的工作呢?

最佳答案

初始化列表对象包含一个指向 3 个 int 数组的指针,该数组的实际值为 1、2、3。当您调用 .begin() 时,返回指向第一个元素(值为 1)的指针。但是,不允许使用指向数组元素的指针作为模板参数,因为标准规定指针类型的模板参数不得指向“子对象”([temp.arg.nontype]/2.1)。不过,我不确定为什么存在此限制。

关于c++ - Constexpr 值作为指针类型的非类型模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57484774/

相关文章:

c++ - std::filesystem::directory_iterator 链接器问题 (C++17)

c++ - 读取文件时堆损坏

c++ - 为什么C/C++中存在单一定义规则

c++ - 范围解析::嵌套模板类型名

宏中的 C++ 模板

c++ - 使用不同的 enable_if 条件选择成员函数

c++ - 在 C++17 中,是否可以使用带有初始值设定项的 if 语句来解包可选?

c++ - 为什么我不能执行 tupleVar.get(3) 或 .get<3>()?

c++ - Qt 5.5 如何围绕位置旋转多边形

c++ - 具有与 char* 相同的别名含义的 std::byte 指针吗?