gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:33:03 25 4
gpt4 key购买 nike

以下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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com