我想在编译期间检查类型是否是特定模板的实例化.
例如:
std::vector<int>
实例化 std::vector
std::array<int, 5>
实例化 std::array
我可以进行适用于案例1的测试,但不适用于案例2.
#include <iostream>
#include <type_traits>
#include <string>
#include <vector>
#include <array>
#include <queue>
template<template<typename...> class, typename...>
struct is_instantiation : public std::false_type {};
template<template<typename...> class U, typename... T>
struct is_instantiation<U, U<T...>> : public std::true_type {};
int main() {
using A = std::vector<int>;
std::cout << is_instantiation<std::vector, A>::value << "\n";
std::cout << is_instantiation<std::queue, A>::value << "\n";
// std::cout << is_instantiation<std::array, A>::value << "\n";
}
Run Code Online (Sandbox Code Playgroud)
如何让它适用于这两种情况?
我试过汽车,但无法使它工作.
我想将const添加到引用类型中typedef const A B;
.
不知怎的,它不起作用.这在c ++中是不可能的吗?
测试:
#include <type_traits>
typedef int& A;
typedef const A B; // <-- Add const
// typedef std::add_const<A>::type B; // also doesn't work.
static_assert(std::is_const<typename std::remove_reference<
B>::type>::value, "is const");
int main() {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
add2.cpp:5:1: error: static assertion failed: is const
static_assert(std::is_const<typename std::remove_reference<
^~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud) 我尝试编写一个迭代器,通过索引迭代容器。AIt
和 aconst It
都允许更改容器的内容。AConst_it
和 aconst Const_it
都禁止更改容器的内容。
之后,我尝试span<T>
在容器上写一个。对于一个类型T
不是常量,这两个const span<T>
和span<T>
允许改变所述容器的内容物。双方const span<const T>
并span<const T>
禁止改变容器的内容。
代码无法编译,因为:
// *this is const within a const method
// But It<self_type> requires a non-const *this here.
// So the code does not compile
It<self_type> begin() const { return It<self_type>(*this, 0); }
Run Code Online (Sandbox Code Playgroud)
如果我让构造函数It
接受一个const
容器,它看起来不对,因为迭代器可以修改容器的内容。
如果我去掉方法的const,那么对于非const 类型T,aconst span<T>
不能修改容器。
It
从继承Const_it
到允许隐式转换It
到Const_it
模板实例化时。 …
给定整数模板参数Key
,搜索方法应提取key == Key
编译期间具有的洋葱层.
如何修复搜索方法?
测试(也在godbolt.org)
#include <iostream>
class A {
public:
static constexpr int key = 0;
template<int s>
constexpr auto& search() const { return *this; }
};
template<class P>
class B {
public:
static constexpr int key = P::key + 1;
template <int Key>
constexpr auto& search() const {
if constexpr (Key == key) return *this;
return p_.search<Key>();
}
P p_;
};
int main() {
B<B<B<B<A>>>> onion;
std::cout << onion.search<1>().key << "\n";
return …
Run Code Online (Sandbox Code Playgroud) 如何从C ++端创建一个numpy数组并将其提供给python?
当返回的数组不再由Python使用时,我希望Python进行清理。
C ++端不会使用delete ret;
释放由分配的内存new double[size];
。
以下正确吗?
#include "pybind11/pybind11.h"
#include "pybind11/numpy.h"
namespace py = pybind11;
py::array_t<double> make_array(const py::ssize_t size) {
double* ret = new double[size];
return py::array(size, ret);
}
PYBIND11_MODULE(my_module, m) {
.def("make_array", &make_array,
py::return_value_policy::take_ownership);
}
Run Code Online (Sandbox Code Playgroud) 在 Xeon CPU (E5-2603) 中,向后内存预取是否与前向内存预取一样快?
我想实现一个需要对数据进行前向循环和后向循环的算法。
由于每次迭代都需要上次迭代的结果,因此我无法颠倒循环的顺序。
谢谢你。
在用 修饰的函数中tf.function
,我尝试调用另一个用 修饰的函数tf.function
。结果慢得可怕。
那是因为我不打算在函数中使用 python 本机类型吗? 使用 tf.function 的 Tensorflow 2.0 模型非常慢,并且每次列车计数发生变化时都会重新编译。Eager 运行速度提高了约 4 倍
测试:
import numpy as np
import tensorflow as tf
@tf.function
def loop(x, y):
for i in range(1000):
x.assign_add(y)
return x
@tf.function
def loop2(x, y):
for i in range(1000):
loop(x, y)
return x
def main():
print("TensorFlow version: {}".format(tf.__version__))
print("Eager execution: {}".format(tf.executing_eagerly()))
x = tf.Variable(initial_value=0, dtype=np.float32)
y = tf.Variable(initial_value=1, dtype=np.float32)
# print(loop2(x, y)) # horribly slow
for i in range(1000): # faster
loop(x, …
Run Code Online (Sandbox Code Playgroud) 在以下代码中,变量定义B<int, int>(14);
应该是错误的:
#include <iostream>
struct A {
};
template<class T, class R=A>
class B {
public:
explicit B(const int s, R n = A()) {
std::cout << "c\n";
}
};
template <class T, class R=A>
void foo(const int s, R nx = A()) {};
int main() {
B<int, int>(14);
// foo<int, int>(14);
// error: could not convert ‘A()’ from ‘A’ to ‘int’
}
Run Code Online (Sandbox Code Playgroud)
为什么它不会导致编译错误?
我用gcc 7.3 编译了代码g++ -std=c++17
当我使用gcc 7.3编译代码时g++ -std=c++14
,我收到一个错误.
我以为该行使用n
构造函数中的参数的默认值 …
以下代码编译.它似乎运行良好.
但它会导致任何未定义的行为吗?
我想抛弃常数*this
.
这是为了允许a const my_iterator
改变它指向的数据.
测试:
class A {
public:
A(const int x) : x_(x) {}
void set_x(int x) { x_ = x; }
void set_x2(const int x) const {
const_cast<A&>(*this).set_x(x);
}
int x_;
};
int main() {
A a(10);
a.set_x2(100);
}
Run Code Online (Sandbox Code Playgroud) 对于 skylakex(agner 雾的说明表):
+-----------------------+-------------+-------------------+---------------------+----------------+---------+-----------------------+
| Instruction | Operands | µops fused domain | µops unfused domain | µops each port | Latency | Reciprocal throughput |
+-----------------------+-------------+-------------------+---------------------+----------------+---------+-----------------------+
| VGETMANTPS/PD | v,v,v | 1 | 1 | p01/05 | 4 | 0.5-1 |
| AND/ANDN/OR/ XORPS/PD | x,x / y,y,y | 1 | 1 | p015 | 1 | 0.33 |
+-----------------------+-------------+-------------------+---------------------+----------------+---------+-----------------------+
Run Code Online (Sandbox Code Playgroud)
这是否意味着使用位掩码和逻辑并获取浮点数的尾数比使用 vgetmantps 指令更快?
将数字从 float 传输到 int 再返回到 float 的延迟是多少?
tf.reset_default_graph()
清除默认图形。退出tf.Session()
上下文时如何清除图形?
示例(pytest):
import tensorflow as tf
def test_1():
x = tf.get_variable('x', initializer=1)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(4 / 0)
print(sess.run(x))
def test_2():
x = tf.get_variable('x', initializer=1)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(x))
Run Code Online (Sandbox Code Playgroud) 我在数组上写二进制堆arr
。
除叶节点外,每个节点都有两个子节点。
根可以是arr[0]
或arr[1]
。
为什么在由数组实现的堆中未使用索引0的可接受答案 ?说arr[1]
更快。
但是在该答案下方的一条评论说,大多数实现都扎根于arr[0]
。
扎根的好处是什么arr[0]
?
c++ ×8
c++17 ×3
python ×3
templates ×3
const ×2
performance ×2
tensorflow ×2
x86 ×2
auto ×1
c++11 ×1
caching ×1
constants ×1
constructor ×1
gcc ×1
heap ×1
intel ×1
optimization ×1
pybind11 ×1
pytest ×1
simd ×1
testing ×1
unit-testing ×1