小编Ant*_*ton的帖子

GCC允许访问私有静态成员

此代码适用于GCC,但不适用于clang.

class Base
{
    static constexpr int PRIVATE = 1;
};

struct Derived : public Base
{
    template <class T>
    int bar( T & t )
    {
        return PRIVATE;
    }
};

int main()
{
    Derived d;
    int i = 3;
    d.bar(i);
}
Run Code Online (Sandbox Code Playgroud)

Godbolt链接:https://godbolt.org/g/qPJ47p

在私有成员函数的情况下,如果模板函数被实例化,GCC正确地检测访问私有成员的尝试,否则不会.即使从未实例化模板函数,Clang也会检测到该尝试.

但是,当使用私有静态constexpr变量时,即使模板函数被实例化,GCC(最新的8.1)也无法停止私有访问.Clang正确(?)抱怨.

问题:在这种情况下,两个编译器中的哪一个符合标准?

在我看来,GCC在允许访问私有静态constexpr变量方面是不对的.然而同时它似乎不是一个过于复杂的问题,但它不是最新的海湾合作委员会:这似乎是故意的.

非常感谢paxdiablo的清晰彻底的回答.根据他的建议,我已经制作了一个更全面的测试用例列表,并将其缩小到static导致GCC问题的说明符.有关更多详细信息,请参阅Godbolt链接:https://godbolt.org/g/A3zCLk

Comparison of GCC and Clang:

Private member    | GCC         | Clang

static const      | accept      | reject
static constexpr  | accept      | reject
static …
Run Code Online (Sandbox Code Playgroud)

c++ gcc clang

11
推荐指数
1
解决办法
356
查看次数

是否可以重载本机数据类型的运算符?

例如,如果我尝试这样做:

a_string + an_int
Run Code Online (Sandbox Code Playgroud)

...其中 a_string 是“str”类型,an_int 是“int”类型,或者:

an_int + a_string
Run Code Online (Sandbox Code Playgroud)

会有 aTypeError因为没有类型的隐式转换。我知道,如果我使用自己的 int 和 string 子类,我将能够重载__add__()我的类中的方法来实现此目的。

但是,出于好奇,我想知道:是否可以在intand的类定义中重载 + 运算符str,以便__add__(int,str)and__add__(str,int)自动将它们连接为字符串?

如果不是,那么程序员不应重载本机数据类型的运算符的原因是什么?

python overloading operator-overloading

7
推荐指数
1
解决办法
1382
查看次数

Python websockets 发送到客户端并保持连接活动

我正在使用 python websockets:https ://websockets.readthedocs.io/

他们有一个简单的客户端/服务器示例,其中服务器将客户端的输入回显一次。代码如下所示:

客户端:

# WS client example

import asyncio
import websockets

async def hello():
    async with websockets.connect(
            'ws://localhost:8765') as websocket:
        name = input("What's your name? ")

        await websocket.send(name)
        print(f"> {name}")

        greeting = await websocket.recv()
        print(f"< {greeting}")

asyncio.get_event_loop().run_until_complete(hello())
Run Code Online (Sandbox Code Playgroud)

服务器端:

# WS server example

import asyncio
import websockets

async def hello(websocket, path):
    name = await websocket.recv()
    print(f"< {name}")

    greeting = f"Hello {name}!"

    await websocket.send(greeting)
    print(f"> {greeting}")

start_server = websockets.serve(hello, 'localhost', 8765)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
Run Code Online (Sandbox Code Playgroud)

我只想调整服务器端,以便它在套接字连接上执行以下操作:

  1. 向客户端发送确认消息。例如Hello Client! Please …

python websocket python-asyncio

7
推荐指数
2
解决办法
1万
查看次数

clang vs gcc CRTP:constexpr 变量不能有非文字类型

我这里有一个 CRTP 模板类:

template <typename S>
class Base
{
    public:
    constexpr static S NOT_SET{0};
};

struct Derived : public Base<Derived>
{
};
Run Code Online (Sandbox Code Playgroud)

Clang (5.0.0) 不接受这一点:

5 : <source>:5:24: error: constexpr variable cannot have non-literal type 'const Derived'
    constexpr static S NOT_SET{0};
                       ^
8 : <source>:8:25: note: in instantiation of template class 'Base<Derived>' requested here
struct Derived : public Base<Derived>
                        ^
5 : <source>:5:24: note: incomplete type 'const Derived' is not a literal type
    constexpr static S NOT_SET{0};
                       ^
8 : …
Run Code Online (Sandbox Code Playgroud)

c++ gcc clang crtp constexpr

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

如何根据Boost版本有选择地包括Boost标头

Boost中的此更改导致某些头文件位置发生更改:https : //github.com/boostorg/property_tree/commit/ea940990691de91e9b22255d9b450fcdac237646

我正在使用一个代码库,其中一些用户使用旧版本的boost和旧的标头位置(例如,#include <boost/property_tree/detail/json_parser_error.hpp>而不是较新的)进行构建#include <boost/property_tree/json_parser/error.hpp>。我想保持与旧版本(1.61之前)和较新版本的兼容性。

如果版本> = 1.61,是否有一种方法指示编译器检查boost版本并使用新的头文件include?

c++ boost c-preprocessor

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

如何使用 zlib 计算 CRC32-C 校验和

我使用 C zlib API 是因为它具有将crc32_combine校验和连接在一起的功能,而 Boost 则没有。

但是,我需要使用 polynomial 来实现 CRC32-C (Castagnoli) 校验和0x1EDC6F41,而不是标准的 CRC32 校验和。

使用 Boost 我显然可以使用:

#include <boost/crc.hpp>

using crc_32c_type = boost::crc_optimal<32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true>;
crc_32c_type result;
result.process_bytes(reinterpret_cast<const char*>(&buffer), len);
return result.checksum();
Run Code Online (Sandbox Code Playgroud)

其中可以使用0x1EDC6F41多项式。

有没有类似的方法可以让我用 zlib 做到这一点?

zlib crc

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

std::string 隐式转换优先级,string_view 优于 const char*

我有两个重载函数:

void Set(const char * str) { std::cout << "const char * setter: " << str << "\n"; }
void Set(const std::string_view & sv) { std::cout << "string_view setter: " << sv << "\n"; }
Run Code Online (Sandbox Code Playgroud)

我在一个std::string. 它选择std::string_view重载而不是 const char * 重载,即它选择std::stringto std::string_viewover std::stringto的隐式转换const char *

这是在 中的保证行为std::string,还是他们的选择是任意的?如果这是一个有保证的行为,他们是如何使它更喜欢 string_view 转换而不是另一个?

c++ stl overload-resolution implicit-conversion c++17

0
推荐指数
1
解决办法
89
查看次数