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

Constexpr value as nontype template parameter of pointer type

以下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,它的 beginendnext。那么是什么阻止了它的工作?

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