小编arm*_*scu的帖子

C++ 中 if/else 分支的编译时消除

在下面的代码示例中,if语句依赖于bool模板参数,它是一个编译时常量。编译器以不同的方式处理此代码:

  • MSVC 因链接错误而失败(这是我所期望的),因为else分支中的模板函数缺乏true模板参数值的专业化(即使它从未被调用)。

  • GCC 和 Clang 编译时都没有问题,并且运行时行为是正确的。这显然是因为它们if在编译时评估语句并在链接之前删除未使用的分支。

问题是哪种行为符合标准(或者它是未定义的行为并且两者都以自己的方式正确)?

#include <iostream>

template<const bool condition>
struct Struct
{
    void print()
    {
        if (condition)
        {
            std::cout << "True\n";
        }
        else
        {
            printIfFalse();
        }
    }

private:
    void printIfFalse();
};

template <>
void Struct<false>::printIfFalse()
{
    std::cout << "False\n";
}

int main()
{
    Struct<true> withTrue{};
    withTrue.print();

    Struct<false> withFalse{};
    withFalse.print();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ templates compiler-optimization template-specialization language-lawyer

12
推荐指数
1
解决办法
626
查看次数

SameSite 属性和 Facebook SDK

我正在尝试解决从 Chrome 浏览器收到的警告:

A cookie associated with a cross-site resource at http://www.facebook.com/ was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032. <br/>
Run Code Online (Sandbox Code Playgroud)

我正在使用 Facebook 登录网络 SDK。当试图摆脱警告时,我创建了一个非常简单的页面,没有 cookie。单个 cookie 不是由我的脚本创建的,而是来自 Facebook 的。看起来后端代码并没有真正影响 cookie。它在响应头中设置。

在 PHP 代码中,我引入了这段代码:

$fbcook = $_COOKIE[$facebookcookiename];
setrawcookie($facebookcookiename, $fbcook, ['samesite' => 'None', 'secure' => true]); …
Run Code Online (Sandbox Code Playgroud)

php facebook http web samesite

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

如何为 &lt;stropts.h&gt; 安装库?

我正在运行 Kali-Linux (debian+gnome)。编译时出现编译错误:

cc -Wall -g   -c -o frag.o frag.c
frag.c:7:10: fatal error: stropts.h: No such file or directory
    7 | #include <stropts.h>
      |          ^~~~~~~~~~~
compilation terminated.
make: *** [<builtin>: frag.o] Error 1
Run Code Online (Sandbox Code Playgroud)

谁能告诉我我缺少什么以及我应该安装什么?我尝试安装 glibc-sources,但仍然没有成功。

c c++ debian gcc

4
推荐指数
1
解决办法
6095
查看次数