小编And*_*nak的帖子

是否有"安全"的static_cast替代方案?

static_cast在C++ 11/14或实现此功能的库中是否有"安全"替代方案?

"安全"我的意思是演员应该只允许不失精度的演员.因此,如果数字适合a ,则仅允许来自int64_tto int32_t的强制转换int32_t,否则报告错误.

c++ safety-critical type-safety narrowing c++11

16
推荐指数
2
解决办法
2051
查看次数

基于引用计数以“错误”顺序调用的 Python 析构函数

As far as I understand Python destructors should be called when the reference count of an object reaches 0. But this assumption seems not to be correct. Look at the following code:

class A:
    def __init__(self, b):
        self.__b = b
        print("Construct A")
        
    def __del__(self):
        # It seems that the destructor of B is called here.
        print("Delete A")
        # But it should be called here
        
class B:
    def __init__(self):
        print("Construct B")
        
    def __del__(self):
        print("Delete B")
        
b = B()
a = A(b) …
Run Code Online (Sandbox Code Playgroud)

python destructor python-3.x

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

在多个线程中重用 Tensorflow 会话会导致崩溃

背景:

我有一些复杂的强化学习算法,我想在多个线程中运行。

问题

尝试调用sess.run线程时,我收到以下错误消息:

RuntimeError: The Session graph is empty. Add operations to the graph before calling run().

重现错误的代码:

import tensorflow as tf

import threading

def thread_function(sess, i):
    inn = [1.3, 4.5]
    A = tf.placeholder(dtype=float, shape=(None), name="input")
    P = tf.Print(A, [A])
    Q = tf.add(A, P)
    sess.run(Q, feed_dict={A: inn})

def main(sess):

    thread_list = []
    for i in range(0, 4):
        t = threading.Thread(target=thread_function, args=(sess, i))
        thread_list.append(t)
        t.start()

    for t in thread_list:
        t.join()

if __name__ == '__main__':

    sess = tf.Session()
    main(sess) …
Run Code Online (Sandbox Code Playgroud)

python multithreading python-multithreading python-3.x tensorflow

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

std :: allocator_traits ::用const指针构造

以下代码可以正常编译:

#include <iostream>
#include <memory>
int main()
{
const int * a = new int(5);

std::cout << *a << std::endl; // Prints 5

// *a = 5; // Compiler error.
using at = std::allocator_traits<typename std::allocator<int>>;
auto alloc = std::allocator<int>();
at::construct(alloc, a);

std::cout << *a << std::endl; // Prints 0
}
Run Code Online (Sandbox Code Playgroud)

隐藏在libstdc ++中

::new((void*)a) int;
Run Code Online (Sandbox Code Playgroud)

但是aconst

这是未定义的行为吗?还是新放置的位置不算作修改?我修改了的值*aconst。据我了解,这是不允许的:

通过非常量访问路径修改const对象,并通过非易失性glvalue引用volatile对象会导致未定义的行为。

https://zh.cppreference.com/w/cpp/language/const_cast

c++ allocator language-lawyer c++11 c++14

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

根据模板是指针/引用还是无模板选择函数

我想提供一个函数的不同实现,它依赖于它是指针,引用还是常规类型.到目前为止这是我的代码:

template<class T,
         class = typename std::enable_if_t<std::is_reference<T>::value>>
void f(T && in)
{}

// This causes redefinition error 
template<class T,
    class = typename std::enable_if_t<std::is_pointer<T>::value>>
void f(T && in)
{}

template<class T,
    class = typename std::enable_if_t<!std::is_reference<T>::value>,
    class = typename std::enable_if_t<!std::is_pointer<T>::value>>
void f(T && in)
{}
Run Code Online (Sandbox Code Playgroud)

中间函数导致:

12:13:错误:重新定义'template void f(T &&)'

7:13:注意:先前在此声明的'template void f(T &&)'

Funnily只有第一个和最后一个函数一起编译.

任何想法如何解决或简化此代码.

c++ sfinae c++11

4
推荐指数
2
解决办法
75
查看次数

CRTP基础构造函数崩溃,因为未构造子级

我有自动生成的类,但我想让最终用户添加自定义成员函数和构造函数.

我的方法是使用一个CRTP基类,它没有成员变量只是函数.

问题在于构造函数.如果我在我的CRTP中定义构造函数,我无法正确访问子节点,因为它尚未构造,因为只有在构造CRTP基础之后才调用子类构造函数.

#include <iostream>
#include <string>

template<class Child>
struct Foo {
  Foo(std::string i) {
    // Childs constructor is not run yet.
    std::cout << static_cast<Child&>(*this).d.size(); // Prints trash
    static_cast<Child&>(*this).d = i; // Segfault here
    (void) i;
  }
};

// Cannot change this class.
struct Bar : Foo<Bar> {
  using base_t = Foo<Bar>;
  using base_t::base_t;
  std::string d;
};


int main()
{
  Bar bar("asdgasdgsag");
  std::cout << "bar.d: " << bar.d << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

有没有办法解决这个问题?

c++ constructor crtp c++11

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

非会员转换,不同类型的同一会员

我有两个我无法修改的类,两个都有完全相同的成员:

class Pose1 {
    public:
    double x,y;
};
class Pose2 {
   public:
   double x,y;
};
Run Code Online (Sandbox Code Playgroud)

代码的一部分使用Pose1,另一部分使用Pose2.有没有办法将这些隐含地转换成彼此?现在我必须一直写

Pose1 p1(0.5, 0.5);
Pose2 p2(p1.x,p2.y);
Run Code Online (Sandbox Code Playgroud)

我知道我可以编写一个只执行一次的转换函数.但是我有很多不同的类型,有很多参数.

有什么方法可以做我喜欢的事情:

Pose2 p2  = static_cast<Pose2>(p1);
Run Code Online (Sandbox Code Playgroud)

我不能使用成员函数,因为我无法更改此代码.

谢谢!

c++ c++11

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