SFINAE 未检测到 T::reference

发布于 2024-11-04 01:55:26 字数 1194 浏览 0 评论 0 原文

std::vector 类是 STL 容器概念的模型,因此任何正确的向量实现都必须包含嵌套的 typedef value_type 以及参考。这应该可以使用 SFINAE 检测到。不过,在我自己的测试中,我可以使用 SFINAE 来检测嵌套的 value_type typedef,但由于某种原因,我无法检测引用

template <class T> 
typename T::value_type* test(T)
{
    cout << "Has nested typedef!" << endl;
}

template <class T> 
void test(...)
{
    cout << "Doesn't have nested typedef!" << endl;
}

int main()
{
    test(std::vector<int>());
}

输出: Hasnested typedef!

但是,如果我将 value_type 替换为 reference,例如:

template <class T> 
typename T::reference* test(T)
{
    cout << "Has nested typedef!" << endl;
}

template <class T> 
void test(...)
{
    cout << "Doesn't have nested typedef!" << endl;
}

int main()
{
    test(std::vector<int>());
}

...程序根本无法编译,给出错误: error: nomatching function for call to test(std::vector>)

为什么 SFINAE 技术适用于 T: :value_type 但不与 T::reference 一起使用?

The std::vector<T> class is a model of the STL Container concept, and as such any proper implementation of vector has to include a nested typedef value_type as well as reference. This should be detectable using SFINAE. However, in my own tests, I can use SFINAE to detect a nested value_type typedef, but for some reason I can't detect reference.

template <class T> 
typename T::value_type* test(T)
{
    cout << "Has nested typedef!" << endl;
}

template <class T> 
void test(...)
{
    cout << "Doesn't have nested typedef!" << endl;
}

int main()
{
    test(std::vector<int>());
}

This outputs: Has nested typedef!

However, if I replace value_type with reference, like:

template <class T> 
typename T::reference* test(T)
{
    cout << "Has nested typedef!" << endl;
}

template <class T> 
void test(...)
{
    cout << "Doesn't have nested typedef!" << endl;
}

int main()
{
    test(std::vector<int>());
}

...the program fails to compile at all, giving the error: error: no matching function for call to test(std::vector<int, std::allocator<int> >)

Why does the SFINAE technique work with T::value_type but not with T::reference?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多像笑话 2024-11-11 01:55:26

什么是指向引用的指针?

A:不可能。指向引用的指针不能存在,因此您的两个函数都不能存在。这与您的第一种情况相反,在第一种情况下,至少有一个函数可以存在(因此您可以获得编译、链接和输出)。

有趣的是,SFINAE 在这里工作,因为函数定义不会导致编译错误。它试图调用一个因不可能+SFIN​​AE 而不存在的函数,这会导致错误。 :)

What's a pointer to a reference?

A: Impossible. Pointers to references cannot exist, so neither of your functions can exist. This is in contrast to your first case, where at least one of the functions can exist (and thus you get compilation, linkage and output).

Interestingly, SFINAE is working here, as the function definition is not causing the compilation error. It's attempting to call a function that, because of impossibility+SFINAE, doesn't exist, that's causing the error. :)

烟火散人牵绊 2024-11-11 01:55:26

类型名称 T::reference* test(T)

指向引用的指针在 C++ 中是非法的。

标准§8.3.2/4 说:

不得有对引用的引用,不得有引用数组,并且不得有指向引用的指针

typename T::reference* test(T)

Pointers to references are illegal in C++.

§8.3.2/4 from the Standard says:

There shall be no references to references, no arrays of references, and no pointers to references.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文