小编ngm*_*r80的帖子

在布尔上下文中使用具有非类型模板参数包的概念

在 C++20 中,我定义了一个AllIntegral带有非类型模板参数包的概念auto... T_values。虽然 GCC 10.1.0 在某些上下文中接受它的用法,但它拒绝编译它在其他上下文中的用法,尤其是在 if语句中。相关的错误消息显示“AllIntegral”不限制类型

我的代码如下所示:

#include <concepts>
#include <ios>
#include <iostream>

template<auto... T_values>
concept AllIntegral = (std::integral<decltype(T_values)> && ...);

int main()
{
    std::cout << std::boolalpha << AllIntegral<1, 2> << '\n';  // compiles and prints "true"
    if (AllIntegral<1, 2>) std::cout << "true" << '\n';        // does not compile

    std::cout.flush();
}
Run Code Online (Sandbox Code Playgroud)

这是编译器输出:

main.cpp: In function ‘int main()’:
main.cpp:11:9: error: ‘AllIntegral’ does not constrain a type
   11 |     if (AllIntegral<1, 2>) std::cout …
Run Code Online (Sandbox Code Playgroud)

c++ c++-concepts c++20 non-type-template-parameter

6
推荐指数
0
解决办法
118
查看次数

指向成员的指针访问运算符的行为差异

在 C++ 中,我正在寻找标准的关键部分\n解释我在该语言的两个指向成员的指针访问运算符之间观察到的行为的细微差别,.*以及->*

\n

根据下面显示的我的测试程序,虽然->*似乎允许其\n右手表达式是任何类型隐式转换为 \n pointer to member of S.*但事实并非如此。当使用 ngcc 和 clang 进行编译时,两个编译器都会对标记为 \'(2)\' 的行产生错误,表明我的类Offset不能用作成员指针。

\n

测试程序\n https://godbolt.org/z/46nMPvKxE

\n
#include <iostream>\n\nstruct S { int m; };\n\ntemplate<typename C, typename M>\nstruct Offset\n{\n    M C::* value;\n    operator M C::* () { return value; }  // implicit conversion function\n};\n\nint main()\n{\n    S s{42};\n    S* ps = &s;\n    Offset<S, int> offset{&S::m};\n\n    std::cout << ps->*offset << \'\\n\';  // (1) ok\n    std::cout << s.*offset …
Run Code Online (Sandbox Code Playgroud)

c++ pointer-to-member language-lawyer implicit-conversion

5
推荐指数
1
解决办法
174
查看次数