执行以下操作的pythonic方法是什么:
我有两个列表a和b相同的长度n,我想形成列表
c = [a[0], b[0], a[1], b[1], ..., a[n-1], b[n-1]]
Run Code Online (Sandbox Code Playgroud) 我有一个模板化的类A和一个f返回A对象的模板化函数.我想f<T>成为朋友,现在A<T>仍然是constexpr
template <typename T>
class A;
template <typename T>
constexpr A<T> f();
//f<T> is a friend of A<T>
template <typename T>
class A {
friend /* constexpr? */ A f<T>();
constexpr A() {}
};
template <typename T>
constexpr A<T> f() { return {}; }
int main() {
constexpr auto a = f<void>();
}
Run Code Online (Sandbox Code Playgroud)
我不能让clang和gcc同意这里的内容.如果我没有constexpr输入好友声明,gcc工作正常但clang不会编译它,错误的:
main.cpp:18:18: error: constexpr variable 'a' must be initialized by a constant expression
constexpr …Run Code Online (Sandbox Code Playgroud) 我有一个线程类,其循环需要每秒执行4次.我知道我可以做类似的事情
do_stuff()
time.sleep(0.25)
Run Code Online (Sandbox Code Playgroud)
但问题是,这是不考虑所需的时间do_stuff().实际上,这需要是一个实时线程.有没有办法实现这个目标?理想情况下,线程在不执行代码时仍会处于休眠状态.
在Ruby中有没有很酷的方法来创建一个1到100的数组,只有奇数条目(1,3等).我现在有一个循环,但这显然不是一个很酷的方式来做到这一点!有什么建议?
我目前的代码:
def create_1_to_100_odd_array
array = [1]
i = 3
while i < 100
array.push i
i += 2
end
array
end
Run Code Online (Sandbox Code Playgroud)
提前致谢
参考限定成员函数的stock示例似乎是这样的:
#include <stdio.h>
#include <stdexcept>
#include <string>
// Easy access to literals
using namespace std::literals;
// File wrapper
class File {
private:
// The wrapped file
FILE *_file;
public:
File(const char *name) :
_file(fopen(name, "r")) {
// unable to open the file?
if (!_file) throw std::runtime_error{ "Unable to open file: "s + name };
}
~File() {
fclose(_file);
}
// Convert to the underlying wrapped file
operator FILE *() & {
return _file;
}
// TODO: Member functions for working …Run Code Online (Sandbox Code Playgroud) 我有一个结尾的文件名,.zip我只想要没有zip的文件名.在这里,我在bash中找到了一个技巧.
$f="05 - Means-End Analysis Videos.zip"
$echo "${f%*.zip}"
05 - Means-End Analysis Videos
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?怎么%*.zip删除我的扩展名?
在wandbox中乱搞我发现clang实际上会发出警告,如果它看到<=>出现在C++ 17或更早版本中.
warning: '<=>' is a single token in C++2a; add a space to avoid a change in behavior [-Wc++2a-compat]
Run Code Online (Sandbox Code Playgroud)
我试图弄清楚如何<=>在C++ 17中编写一个合法的字符序列用例,但我想出的都觉得非常有用.最可能的例子(imo)涉及使用模板:
struct A {
bool operator<=(A) const { return true; }
};
template <auto Cmp>
void f() { }
int main() {
f<&A::operator<=>();
}
Run Code Online (Sandbox Code Playgroud)
其他所有内容仍涉及按名称明确提及比较功能operator<=.是否有一个更常见的外观<=>,我没想到哪个会激励clang开发人员添加此警告?
我想创建一个可以用作的命令行标志
./prog.py --myarg=abcd,e,fg
Run Code Online (Sandbox Code Playgroud)
并在解析器内部将其转换为['abcd', 'e', 'fg'](元组也可以)。
我已经使用actionand成功地做到了这一点type,但我觉得一个可能是对系统的滥用或遗漏了极端情况,而另一个是正确的。但是,我不知道哪个是哪个。
与action:
import argparse
class SplitArgs(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, values.split(','))
parser = argparse.ArgumentParser()
parser.add_argument('--myarg', action=SplitArgs)
args = parser.parse_args()
print(args.myarg)
Run Code Online (Sandbox Code Playgroud)
取而代之的是type:
import argparse
def list_str(values):
return values.split(',')
parser = argparse.ArgumentParser()
parser.add_argument('--myarg', type=list_str)
args = parser.parse_args()
print(args.myarg)
Run Code Online (Sandbox Code Playgroud) 编辑追加:问题标题是"做Visual Studio编译器或Clang有不正确的行为" - 但是已经改变了.
所以我在这里补充说clang和gcc按照我的意图编译它,但VS没有.
我有以下代码:
template<typename S, typename T, std::size_t... I>
void
print_tuple_like(S& s, const T& t, std::index_sequence<I...>)
{
void* unused[] = { &(s << std::get<I>(t))... };
}
template<typename S, typename T,
std::size_t N = std::tuple_size<decltype(T::children)>::value>
S& operator<<(S& s, const T& t)
{
print_tuple_like(s, t.children, std::make_index_sequence<N>{});
return s;
}
Run Code Online (Sandbox Code Playgroud)
我收到编译器错误:
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\utility(313): error C2338: The C++ Standard doesn't define tuple_size for this type.
1> c:\users\jonas\documents\visual studio 2015\projects\consoleapplication7\consoleapplication7\consoleapplication7.cpp(36): note: see reference to class template instantiation 'std::tuple_size<unknown-type>' …Run Code Online (Sandbox Code Playgroud) 以下无法在c ++ 14中的gcc和clang下编译,但是使用c ++ 1z成功:
struct Cls {
static constexpr int N = 0;
};
constexpr int Cls::N;
constexpr int Cls::N;
Run Code Online (Sandbox Code Playgroud)
C++ 14错误是可预测的: redefinition of ‘constexpr const int Cls::N’
是什么改变使这合法?我发现:
n4659 10.1.5 [dcl.constexpr]
使用constexpr说明符声明的函数或静态数据成员隐式地是内联函数或变量
所以我认为它可能与内联变量有关,但是在两个编译器下c ++ 1z的后续失败
struct Cls {
static inline const int N = 0;
};
inline const int Cls::N; // note, only one definition here
Run Code Online (Sandbox Code Playgroud)