小编cxx*_*xxl的帖子

Visual C++是否有__builtin_constant_p()?

是否有像__builtin_constant_p()Microsoft Visual Studio的GCC一样的功能?据我所知,如果参数是常量,函数返回非零,就像字符串文字一样.

在这里的答案(如何拥有"constexpr和运行时"别名)是一个很好的用例.

编辑: 我的想法不是写一些像:

#include <string.h>
int foo() {
   return strlen("text");
}
Run Code Online (Sandbox Code Playgroud)

我可以写:

#include <string.h>
// template_strlen() would be a function that gets the length of a compile-time     const string via templates
#define STRLEN(a) (__builtin_constant_p(a) ? template_strlen(a) : strlen(a))
int foo() {
   return STRLEN("text");
}
Run Code Online (Sandbox Code Playgroud)

(我猜这是关于在链接问题中写的内容.)我需要的只是一个变体__builtin_constant_p().

c++ windows visual-studio visual-c++

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

如何处理电子邮件包中的 Python 3.x UnicodeDecodeError?

我尝试从文件中读取电子邮件,如下所示:

import email
with open("xxx.eml") as f:
   msg = email.message_from_file(f)
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

Traceback (most recent call last):
  File "I:\fakt\real\maildecode.py", line 53, in <module>
    main()
  File "I:\fakt\real\maildecode.py", line 50, in main
    decode_file(infile, outfile)
  File "I:\fakt\real\maildecode.py", line 30, in decode_file
    msg = email.message_from_file(f)  #, policy=mypol
  File "C:\Python33\lib\email\__init__.py", line 56, in message_from_file
    return Parser(*args, **kws).parse(fp)
  File "C:\Python33\lib\email\parser.py", line 55, in parse
    data = fp.read(8192)
  File "C:\Python33\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 1920: character maps …
Run Code Online (Sandbox Code Playgroud)

python unicode exception python-3.x python-unicode

5
推荐指数
2
解决办法
2771
查看次数

如何用std :: bind()创建数据成员?

我用C++ 11很好的新生成器和发行版生成随机值.在一个函数中,它就像一个魅力,看起来像这样:

void foo() {
   mt19937 generator;
   uniform_int_distribution<unsigned> distribution;
   auto dice = bind(distribution, generator);
   // dice() will now give a random unsigned value
}
Run Code Online (Sandbox Code Playgroud)

但是,如何将所有三个对象作为数据成员放在一个类中呢?我可以简单地编写generatordistribution作为数据成员,但是如何在dice不知道(或想知道)其确切类型的情况下创建数据成员?这令人惊讶

class X {
   mt19937 generator;
   uniform_int_distribution<unsigned> distribution;
   decltype(bind(distribution, generator)) dice;
};
Run Code Online (Sandbox Code Playgroud)

error C2660: 'bind' : function does not take 2 arguments在Visual Studio 2013中产生错误.

c++ member stdbind c++11

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

可取消的异步键盘输入

我正在尝试编写一个并发的Python程序,asyncio它也接受键盘输入。当我尝试关闭程序时出现问题。由于键盘输入最终是通过 完成的sys.stdin.readline,因此该函数仅在我按下 后返回ENTER,无论我是stop()事件循环还是cancel()函数的Future

有什么办法可以提供asyncio可以取消的键盘输入吗?

这是我的 MWE。它将接受键盘输入 1 秒,然后stop()

import asyncio
import sys

async def console_input_loop():
    while True:
        inp = await loop.run_in_executor(None, sys.stdin.readline)
        print(f"[{inp.strip()}]")

async def sleeper():
    await asyncio.sleep(1)
    print("stop")
    loop.stop()

loop = asyncio.get_event_loop()
loop.create_task(console_input_loop())
loop.create_task(sleeper())
loop.run_forever()
Run Code Online (Sandbox Code Playgroud)

python python-asyncio

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

在 C++ 中检查多个值是否相等

我正在 C++ 中寻找一种简单、快速和描述性的方法来检查一个值是否包含在一组固定的其他值中。就像在 Python 中一样,可以在其中编写

if some_function() in (2, 3, 5, 7, 11):
    do_something()
Run Code Online (Sandbox Code Playgroud)

一些明显的选择是:

  1. switch/ case:如果有问题的值是整数,那么可以这样写:

    switch (some_function()) {
        case 2: case 3: case 5: case 7: case 11: do_something();
    }
    
    Run Code Online (Sandbox Code Playgroud)

    不幸的是,这只适用于整数,我敢说它不是很漂亮。

  2. 使用局部变量保存临时结果:

    const auto x = some_function();
    if (x == 2 || x == 3 || x == 5 || x == 7 || x == 11) do_something();
    
    Run Code Online (Sandbox Code Playgroud)

    我想避免命名临时变量。此外,这编写起来很乏味并且容易出错。

  3. 使用std::set:这可以写成(至少在 C++20 中)为:

    if (std::set({ 2, 3, 5, 7, 11 }).contains(some_function())) do_something();
    
    Run Code Online (Sandbox Code Playgroud)

    这很好,但我担心它有一些沉重的 STL 开销。 …

c++ templates

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

Django:模板中if条件的问题

我想根据当前的语言代码打印一些东西.为此,我做了这样的事情:

{% if request.LANGUAGE_CODE == en %}
    <h1>English</h1>
{% endif %}
Run Code Online (Sandbox Code Playgroud)

但是这个if条件不能比较当前的语言代码.但是,如果我{{request.LANGUAGE_CODE}}在同一页面打印它然后它将打印en为语言代码,但我的if条件不起作用,我不知道为什么?

python django templates if-statement lang

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

模板化类型的模板专业化

我有一个结构,其中包含有关类型的特征:

template<typename T> struct x_trait { static const bool has_x = true; };
Run Code Online (Sandbox Code Playgroud)

这对于除特定模板类型之外的所有类型都是正确的。对于某些模板类型,我想更改特征:

template<> struct x_trait<tt_type<int>> { static const bool has_x = false; };
Run Code Online (Sandbox Code Playgroud)

到目前为止,一切都很好。但是其tt_type本身采用不同的模板参数。有没有办法x_trait为所有模板化tt_types设置?现在,我唯一的出路是列出所有类型:

template<> struct x_trait<tt_type<char>> { static const bool has_x = false; };
template<> struct x_trait<tt_type<short>> { static const bool has_x = false; };
template<> struct x_trait<tt_type<int>> { static const bool has_x = false; };
template<> struct x_trait<tt_type<long>> { static const bool has_x = false; };
Run Code Online (Sandbox Code Playgroud)

c++ templates template-specialization

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