veh*_*kul 3 c++ templates constexpr c++17
以下代码
#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;
}
Run Code Online (Sandbox Code Playgroud)
产生一些非常奇怪的错误消息:
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
Run Code Online (Sandbox Code Playgroud)
我相信可以编译,因为编译器在编译时具有所有信息:listconstexpr,其begin,end和也是如此next。那么什么阻止了它的工作呢?
初始化列表对象持有一个指向3 ints 数组的指针,该数组实际上具有值1、2、3。调用时.begin(),将返回指向第一个元素(值为1)的指针。但是,由于标准规定指针类型的模板参数不得指向“子对象”([temp.arg.nontype] /2.1),因此不允许使用指向数组元素的指针作为模板参数。我不确定为什么存在此限制。
| 归档时间: |
|
| 查看次数: |
122 次 |
| 最近记录: |