小编R z*_* zu的帖子

检查类型是否是模板的实例化

我想在编译期间检查类型是否是特定模板的实例化.

例如:

  1. std::vector<int> 实例化 std::vector
  2. 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)

如何让它适用于这两种情况?

我试过汽车,但无法使它工作.

C++中模板参数中auto的优点17

c++ templates auto c++17

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

添加const到引用

我想将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)

c++ templates const

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

const、span 和迭代器问题

我尝试编写一个迭代器,通过索引迭代容器。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到允许隐式转换ItConst_it模板实例化时。 …

c++ constants c++11

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

在班级的洋葱中搜索

给定整数模板参数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++ templates c++17

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

Pybind11:从C ++端创建并返回numpy数组

如何从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)

c++ python pybind11

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

至强 CPU (E5-2603) 后向内存预取

在 Xeon CPU (E5-2603) 中,向后内存预取是否与前向内存预取一样快?

我想实现一个需要对数据进行前向循环和后向循环的算法。

由于每次迭代都需要上次迭代的结果,因此我无法颠倒循环的顺序。

谢谢你。

optimization performance x86 caching intel

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

嵌套的 tf.function 非常慢

在用 修饰的函数中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)

python tensorflow tensorflow2.0

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

具有错误类型默认值的构造函数不会引发GCC 7错误

在以下代码中,变量定义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构造函数中的参数的默认值 …

c++ gcc constructor c++17

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

抛弃了这个导致未定义行为的const?

以下代码编译.它似乎运行良好.

但它会导致任何未定义的行为吗?

我想抛弃常数*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)

c++ const undefined-behavior

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

vgetmantps vs andpd 获取浮点数尾数的指令

对于 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 的延迟是多少?

floating-point performance x86 simd

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

在单元测试中退出 tf.Session() 时重置默认图

  • 在每个单元测试结束时,我调用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)

python testing unit-testing pytest tensorflow

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

根为arr [0]的二进制堆有什么好处?

我在数组上写二进制堆arr

除叶节点外,每个节点都有两个子节点。

根可以是arr[0]arr[1]

为什么在由数组实现的堆中未使用索引0的可接受答案 arr[1]更快。

但是在该答案下方的一条评论说,大多数实现都扎根于arr[0]

扎根的好处是什么arr[0]

c++ heap

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