考虑一下这个C++ 11代码:
#include <functional>
#include <cstdlib>
template <typename F>
void test(F &&f) {
auto foo = [f]() {
f();
};
foo();
}
int main() {
test(std::bind(std::puts, "hello"));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
GCC和Clang接受此作为有效的C++ 11代码,但Visual Studio 2013要求将lambda声明为mutable(auto foo = [f]() mutable { ... }).否则我收到此错误:
错误C3848:具有类型'
const std::_Bind<true,int,int (__cdecl *const )(const char *),const char (&)[6]>'的表达式会丢失一些const-volatile限定符以便调用'int std::_Bind<true,int,int (__cdecl *const )(const char *),const char (&)[6]>::operator ()<>(void)'
Visual Studio是否正确拒绝此代码而没有可变,或者它是否有效C++ 11?
(好奇Clang拒绝代码,如果你明显改变std::bind(std::puts, "hello"),std::bind(std::exit, 0)因为它认为noreturn使函数类型不同;我很确定这是一个错误.)
相关问题/材料:
如何确定一个数字是否是正则表达式的素数?(它涉及一元素数匹配,而我正在寻找≥2的基数;不过是一个很好的伎俩,是什么让我思考这个)
http://nikic.github.io/2012/06/15/The-true-power-of-regular-expressions.html
众所周知,由各种编程语言支持的"正则表达式"生成在形式意义上非常规的语言,并且如上述材料所示,能够识别至少一些上下文敏感语言.
语言L = {x | x是基数10中的素数.}是一种上下文敏感语言,因为素数可以通过线性有界自动机来测试(但它不是通过泵浦引理参数的无上下文语言).
那么,是否可以编写一个Perl或Java正则表达式,它恰好接受基数为10的所有素数?如果感觉更容易,可以随意替换≥2的任何其他基础或精确识别所有复合数字.
例如,使用转义来运行任意Perl代码被视为作弊.重复替换(很容易图灵完成)也超出了范围; 整个工作应该在正则表达式内完成.这个问题更多的是关于正则表达式实际有多强大的界限.
我们foo是一个结构或类与拷贝赋值运算符:
struct foo {
foo &operator=(const foo &); // or with some other return type?
};
Run Code Online (Sandbox Code Playgroud)
是否有过一个合理的退货理由比其他任何东西*this从operator=()?将它用于与任务无关的事情并不合理.
在这段代码中,为什么A的构造函数没有参数没有被继承?是否有一个特殊规则阻止继承没有参数的构造函数?
struct A {
A(void *) {}
A() {}
};
class B : public A {
public:
using A::A;
B(int x) {}
};
void f() {
B b(1);
B b2(nullptr);
B b3; // error
}
Run Code Online (Sandbox Code Playgroud)
clang ++ -std = c ++ 11给出了这个错误,g ++ -std = c ++ 11给出了一个非常类似的错误信息:
td.cpp:15:7: error: no matching constructor for initialization of 'B'
B b3; // error
^
td.cpp:9:5: note: candidate constructor not viable: requires single argument 'x', but no arguments
were provided
B(int x) {} …Run Code Online (Sandbox Code Playgroud) Suppose we have
How do I index A by B so that the result is an array of shape (m,), with values taken from the positions indicated by the columns of B?
For example, consider this code that does what I want when B is a python list:
>>> a = np.arange(27).reshape(3,3,3)
>>> a[[0, 1, 2], [0, 0, 0], [1, 1, 2]]
array([ 1, 10, 20]) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用函数来扩充Haskell的Attoparsec解析器库
takeRegex :: Regex -> Parser ByteString
Run Code Online (Sandbox Code Playgroud)
使用其中一个正则表达式实现.
(动机:好的正则表达式库可以提供与输入长度成线性关系的性能,而attoparsec需要回溯.我的输入的一部分特别适合使用正则表达式进行解析,甚至回溯Text.Regex.PCRE库让我该作品的attoparsec代码加速4倍.)
Attoparsec 曾经有一个getInput :: Parser ByteString函数来获取(消耗掉)剩余的输入; 这可能对我的目的来说非常完美,因为我的输入是非增量的,严格且相当小的 - 我一次运行解析器获取一行日志文件.有了它,似乎我可以做类似的事情
takeRegex re = do
input <- getInput
m <- matchM re input
take $ length m
Run Code Online (Sandbox Code Playgroud)
不幸的是,最近版本的attoparsec缺乏此功能.有没有办法实现同样的目标?为什么删除该功能?
现在有一个takeByteString :: Parser ByteString函数,它接受并消耗其余的输入.如果有一个函数来尝试解析并获得结果而不实际消耗任何东西,那么这可以与它一起使用,但我似乎无法找到(或弄清楚如何实现)这样的函数.
有没有办法用当前版本的attoparsec实现这一目标?
到目前为止,在我看来,在 C++ 模块接口中包含几乎所有 libstdc++ 标头都会导致 clang 14.0.0 和与 GCC 11.2.0 捆绑在一起的 libstdc++ 出现编译错误。我想知道我是否做错了什么,或者这是否还不受支持。(我发现Clang 模块支持是“部分”的,但无法找到已实现的内容和未实现的内容。)
这是一个简单的模块示例,我在 Linux 中使用 clang-14,并与 libstdc++ 链接。它演示了 libstdc++ 标头可以在模块实现中使用,但此示例未在模块接口中 #include 任何内容:
// mod_if.cc
export module mod;
export int foo();
// mod.cc
module;
#include <iostream>
module mod;
int foo() {
std::cout << "Hello world from foo()" << std::endl;
return 42;
}
// use.cc
import mod;
#include <iostream>
int main() {
std::cout << foo() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这有效:
$ CXXFLAGS="-std=c++20 -fmodules -fprebuilt-module-path=prebuilt"
$ …Run Code Online (Sandbox Code Playgroud) c++ ×4
c++11 ×2
regex ×2
arrays ×1
attoparsec ×1
c++-modules ×1
clang ×1
constructor ×1
haskell ×1
idioms ×1
inheritance ×1
java ×1
lambda ×1
libstdc++ ×1
numpy ×1
parsing ×1
pcre ×1
python ×1