小编Chr*_*s_F的帖子

使用DOMParser.parseFromString时是否可以提供baseURI?

DOMParser用来解析包含相对URL的文档,用于诸如表单的action属性之类的事情。因为由创建的文档的baseURI DOMParser正在null访问action属性,所以会产生一个空白字符串。我可以通过使用来解决这个问题,getAttribute但是如果可以使用baseURI来指定它DOMParser,那将是理想的选择。

javascript

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

使用模板键入punning cast

我想知道以下代码是否是一种可接受的方法,以不破坏严格别名规则的方式处理类型惩罚.我意识到这个方法依赖于GCC编译器扩展,所以没有必要指出这一点.

template <class output_type, class input_type>
inline output_type punning_cast(const input_type& input)
{
    static_assert(std::is_pod<output_type>::value, "output_type for punning_cast must be POD");
    static_assert(std::is_pod<input_type>::value, "input_type for punning_cast must be POD");
    static_assert(sizeof(output_type) == sizeof(input_type), "input_type and output_type must be the same size");

    typedef output_type __attribute__((may_alias)) output_type_may_alias;

    return *reinterpret_cast<const output_type_may_alias*>(&input);
}

template <class output_type, class input_type>
inline output_type punning_cast(const input_type* input)
{
    static_assert(std::is_pod<output_type>::value, "output_type for punning_cast must be POD");
    static_assert(std::is_pod<input_type>::value, "input_type for punning_cast must be POD");

    typedef output_type __attribute__((may_alias)) output_type_may_alias;

    return *reinterpret_cast<const output_type_may_alias*>(input);
}
Run Code Online (Sandbox Code Playgroud)

用法示例:

uint32_t …
Run Code Online (Sandbox Code Playgroud)

c++ gcc casting type-punning c++11

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

可以高频调用 std::async 吗?

我写了一个std::async用于并行性的小程序,它在我身上崩溃了。我很确定有更好的方法来做到这一点,但现在我只想知道这里发生了什么。我不打算发布确切的代码,因为我认为它没有真正的区别。它基本上看起来像这样:

while(1)
{
    std::vector<Things> things(256);

    auto update_the_things = [&](int start, int end) { //some code };

    auto handle1 = std::async(std::launch::async, update_the_things, 0, things.size() / 4);
    auto handle2 = std::async(std::launch::async, update_the_things, things.size() / 4, things.size() / 4 * 2);
    auto handle3 = std::async(std::launch::async, update_the_things, things.size() / 4 * 2, things.size() / 4 * 3);
    update_the_things(things.size() / 4 * 3, things.size());

    handle1.get();
    handle2.get();
    handle3.get();
}
Run Code Online (Sandbox Code Playgroud)

该循环每秒运行数千次,在随机时间(5 秒 - 1 分钟)后崩溃。如果我查看任务管理器,我会发现该程序的线程数正在迅速波动,这让我认为std::async每次调用都会启动新线程。我原以为它可以与线程池或其他东西一起使用。无论如何,这是因为我做错了什么而崩溃吗?

使用 GDB 我得到以下信息:

Program received signal SIGSEGV, Segmentation …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading asynchronous c++11

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

是否有可能在Promise.all中捕获所有被拒绝的承诺?

鉴于以下内容

Promise.all(promises).then(resolved => {
    ...
}).catch(rejected => {
    ...
});
Run Code Online (Sandbox Code Playgroud)

rejected只会包含被拒绝的第一个承诺.有没有办法捕捉所有被拒绝的承诺?

javascript ecmascript-6 es6-promise

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

我应该更喜欢命名空间还是具有静态函数的类?

在 TypeScript 中,有两种可能的方法来捆绑和公开一组函数。一种是导出一个只包含公共静态函数的类。另一种方法是创建命名空间,然后从其中导出函数。据我所知,这会在 TypeScript 中产生相同的行为(尽管可能会生成不同的 JavaScript)。是否有一种首选方法,或者它很大程度上取决于个人喜好。

namespace MyCollection {
    export function doSomething(macguffin: any) {
        //todo: implement doSomething
    }
}

export class MyCollection {
    public static doSomething(macguffin: any) {
        //todo: implement doSomething
    }
}
Run Code Online (Sandbox Code Playgroud)

typescript

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

使用OpenGL核心配置文件时为什么会崩溃?

当我尝试运行这个简单的OpenGL测试程序时,我遇到了分段错误.只有在使用核心配置文件标志创建上下文时才会发生这种情况.如果我使用兼容性配置文件标志,程序运行没有问题.

编辑:我检查了函数的指针glGenVertexArrays,然后返回NULL.如果glfwCreateWindow没有返回NULL,并glGetString(GL_VERSION)确认上下文是版本4.3并glewInit返回GLEW_OK那么为什么glGenVertexArrays == NULL

我的操作系统是64位Windows 7,而我的GPU是带有331.82 WHQL驱动程序的Nvidia GTX 760.

码:

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <stdio.h>

#define GLSL(src) "#version 430 core\n" #src

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}

GLuint create_program(const char* vertex_source, const char* fragment_source)
{
    GLuint vs = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vs, 1, &vertex_source, NULL);
    glCompileShader(vs); …
Run Code Online (Sandbox Code Playgroud)

c++ opengl nvidia

3
推荐指数
1
解决办法
6646
查看次数

是否可以在编译时生成字符串?

在下面的示例中,我snprintf在模板函数内部使用来创建包含模板参数值的字符串N。我想知道是否有办法在编译时生成这个字符串。

template <unsigned N>
void test()
{
    char str[8];
    snprintf(str, 8, "{%d}", N);
}
Run Code Online (Sandbox Code Playgroud)

c++ templates string-formatting compile-time c++11

3
推荐指数
1
解决办法
723
查看次数

std::span 构造函数是否缺少 noexcept?

根据cppreference的构造函数(2)std::span定义为

template< class It >
explicit(extent != std::dynamic_extent)
constexpr span( It first, size_type count );
Run Code Online (Sandbox Code Playgroud)

例外情况列为2) Throws nothing.

如果这个构造函数“什么都不抛出”,那么为什么它甚至被列在异常下,为什么构造函数没有标记为 noexcept?

c++ stl exception noexcept

3
推荐指数
1
解决办法
136
查看次数

为什么这只迭代HTMLCollection的奇数元素?

当使用for ... of循环迭代从DOMParser返回的HTMLCollection时,只返回奇数元素.我不认为NodeLists或HTMLCollections 不是用DOMParser创建的.如果我将HTMLCollection转换为数组也不会发生这种情况.

知道为什么会这样吗?

if (!HTMLCollection.prototype[Symbol.iterator]) {
    HTMLCollection.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
}

let parser = new DOMParser();
let markup = '<p>Node 1</p><p>Node 2</p><p>Node 3</p><p>Node 4</p><p>Node 5</p>';
let doc = parser.parseFromString(markup, "text/html");
for (let element of doc.body.children) {
    document.body.appendChild(element);
}
Run Code Online (Sandbox Code Playgroud)

javascript dom ecmascript-6

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

为什么在这种情况下接口合并不起作用?

我正在 Visual Studio Code 中编写一些 WebGL 代码(类型声明来自 npm 包 @types/webgl2)并且 typescript 似乎没有合并以下接口。

interface WebGL2RenderingContext {
    myExtension(): void
}

const gl: WebGL2RenderingContext = canvasElem.getContext("webgl2")
gl.myExtension() //getting a TS error saying 'myExtension' does not exist on type WebGL2RenderingContext
Run Code Online (Sandbox Code Playgroud)

谁能解释为什么这不起作用?

编辑:所以我可能应该包括extends WebGLRenderingContext,因为这WebGL2RenderingContext是定义接口的方式。但是,它仍然没有像我预期的那样工作。

interface WebGL2RenderingContext extends WebGLRenderingContext {
    myExtension(): void
}

const gl: WebGL2RenderingContext = canvasElem.getContext("webgl2")
gl.myExtension() //works
gl.createTexture() //works
gl.createVertexArray() //getting a TS error saying 'createVertexArray' does not exist on type WebGL2RenderingContext

//note: createTexture is present in WebGLRenderingContext …
Run Code Online (Sandbox Code Playgroud)

typescript visual-studio-code

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