我正在写一个类似于类型擦除的函数包装器std::function.(是的,我已经看过类似的实现,甚至是p0288r0提案,但我的用例非常狭窄且有些专业化.).以下大量简化的代码说明了我当前的实现:
class Func{
alignas(sizeof(void*)) char c[64]; //align to word boundary
struct base{
virtual void operator()() = 0;
virtual ~base(){}
};
template<typename T> struct derived : public base{
derived(T&& t) : callable(std::move(t)) {}
void operator()() override{ callable(); }
T callable;
};
public:
Func() = delete;
Func(const Func&) = delete;
template<typename F> //SFINAE constraints skipped for brevity
Func(F&& f){
static_assert(sizeof(derived<F>) <= sizeof(c), "");
new(c) derived<F>(std::forward<F>(f));
}
void operator () (){
return reinterpret_cast<base*>(c)->operator()(); //Warning
}
~Func(){
reinterpret_cast<base*>(c)->~base(); //Warning
} …Run Code Online (Sandbox Code Playgroud) 我遇到一个编译错误,说:
试图引用已删除的功能
#include <iostream>
#include <vector>
template <typename T>
struct Container
{
Container() = default;
Container(const Container& other) = delete;
Container(T* ptr) : ptr(ptr) {}
T* ptr;
~Container() { delete ptr; }
};
struct Foo { Foo(int a, int b) {} };
int main()
{
std::vector<Container<Foo>> myvector;
myvector.push_back(new Foo(1, 2)); // I understand why this doesn't work.
myvector.emplace_back((new Foo(1, 2))); // I don't understand why this fails
}
Run Code Online (Sandbox Code Playgroud)
我理解为什么它会在我做的时候尝试引用已删除的构造函数std::vector::push_back(),因为这会复制并需要调用我删除的复制构造函数.
但是std::vector::emplace_back()应该采用它所拥有的类型的构造函数参数.当我向后移动时,我给它一个指向a的指针Foo,这应该转发给Container::Container(T* …
考虑以下代码:
struct X{
explicit X(){}
explicit X(const X&){}
};
void foo(X a = X()){}
int main(){}
Run Code Online (Sandbox Code Playgroud)
使用C++ 14标准,GCC 7.1和clang 4.0都拒绝代码,这正是我所期望的.
但是,使用C++ 17(-std=c++1z),它们都接受代码.什么规则变了?
对于两个编译器都表现出同样的行为,我怀疑这是一个bug.但据我所知,最新的草案仍然说,默认参数使用复制初始化 1的语义.同样,我们知道explicit构造函数只允许直接初始化 2.
我正在使用Max OS X 10.10.3,我终于graphics.py在Python 3中展示了它之前没有出现任何模块.
但是,现在当我尝试import graphics,或者from graphics import *,我收到消息:
"source code string cannot contain null bytes"
Run Code Online (Sandbox Code Playgroud)
是否有任何Mac用户(使用Python 3)可能知道什么是错的?有人用过Zelle书和他的graphics.py模块吗?谢谢.
我注意到Arrays的执行速度远远超过Haxe的链接列表(至少在cpp上).我得到的结果如下.
Main.hx:40: With 1 items, Array is 14% faster than List.
Main.hx:40: With 5 items, Array is 58% faster than List.
Main.hx:40: With 10 items, Array is 59% faster than List.
Main.hx:40: With 100 items, Array is 54% faster than List.
Main.hx:40: With 1000 items, Array is 56% faster than List.
Main.hx:40: With 10000 items, Array is 55% faster than List.
Main.hx:40: With 100000 items, Array is 52% faster than List.
Run Code Online (Sandbox Code Playgroud)
这让我感到尴尬.尽管Array必须不断复制项目,但Array怎么能这么快?为什么甚至使用Lists呢?
package tests;
import haxe.Timer;
class Main
{
static …Run Code Online (Sandbox Code Playgroud) 我有一个列表Foo。Foo 具有属性Bar和Lum. 有些Foos 具有相同的 值Bar。如何使用 lambda/linq 对我的FoosBar进行分组,以便我可以迭代每个分组的Lums?
有人可以解释为什么以下工作,尝试以下代码,它工作正常.
class A {
public :
int32_t temp ;
A ( bool y = false ) { }
} ;
int main ( int argc, char *argv[] )
{
A temp ;
temp = new A () ;
temp.temp = 5 ;
std::cout << " " << temp.temp << std::endl ;
return EXIT_SUCCESS;
} // ---------- end of function main ----------
Run Code Online (Sandbox Code Playgroud) 在对数据应用SVM之前,我想通过PCA减小其尺寸。我应该分开Train data,Test data然后分别对它们分别应用PCA还是对组合在一起的两组应用PCA,然后将它们分开?
我有一个从这个表达式中检索到的对象:
const element = document.querySelector("...my selector...");
Run Code Online (Sandbox Code Playgroud)
我需要获得具有某些属性的所有子元素,我知道获得所有孩子的唯一方法是:
const children = Array.from(element.childNodes);
Run Code Online (Sandbox Code Playgroud)
但现在每个childin children都不是一个元素,而是一个节点,因此,我无法使用getAttribute('')它们;
我如何"投" Node到一个Element?,或者有更好的方法吗?