小编Wei*_*ieh的帖子

如何断言constexpr if else子句永远不会发生?

如果条件为真,我想在非constexpr时引发编译时错误,例如:

if constexpr(condition1){
    ...
} else if constexpr (condition2) {
   ....
} else if constexpr (condition3) {
  ....
} else {
    // I want the else clause never taken. But I heard the code below is not allowed
    static_assert(false);
}

// I'd rather not repeat the conditions again like this:
static_assert(condition1 || condition2 || condition3);
Run Code Online (Sandbox Code Playgroud)

c++ c++17

30
推荐指数
3
解决办法
1437
查看次数

如何缩小嵌套属性

我正在使用terser-js缩小我的代码.

输出:

a.prototype.a = ...
a.prototype.b = ...
a.prototype.c = ...
Run Code Online (Sandbox Code Playgroud)

我想要的是:

var h = a.prototype
h.a = ...
h.b = ...
h.c = ...
Run Code Online (Sandbox Code Playgroud)

请注意,我无法手动编写它,因为输入是从中生成的TypeScript.

javascript uglifyjs

20
推荐指数
1
解决办法
236
查看次数

将void(*p)(SomeType*)转换为void(*p)(void*)是否安全?

假设我有如下函数:

void fun(void* p){
    SomeType* p = reinterpret_cast<SomeType*>(p);
    ...
}
Run Code Online (Sandbox Code Playgroud)

api要求签名.我只是想知道我可以把它写成.

void fun(SomeType* p){
    ...
}
Run Code Online (Sandbox Code Playgroud)

把它投到void (*)(void*).

c++

10
推荐指数
1
解决办法
198
查看次数

rvalue数据成员初始化:聚合初始化与构造函数

对于以下代码:

#include <iostream>
#include <memory>
#include <string>
using namespace std;

struct Foo {
    string tag;

    Foo(string t): tag(t){
        cout << "Foo:" << tag << endl;
    }
    ~Foo() {
        cout << "~Foo:" << tag << endl;
    }
};

struct Bar {
    Foo&& foo;
};

struct Baz{
    Foo&& foo;
    Baz(Foo&& f):foo(std::move(f)){

    }
};

int main() {
    Bar bar{Foo("Bar")};
    Baz baz{Foo("Baz")};
    cin.get();
}
Run Code Online (Sandbox Code Playgroud)

结果(g ++ 7.1.0):

Foo:Bar
Foo:Baz
~Foo:Baz
Run Code Online (Sandbox Code Playgroud)

我们可以看到bar成功延长了临时Foo的生命周期但baz未能成功延长.两者有什么区别?如何Baz正确实现构造函数?

编辑:实际上VC++ 2017给出:

Foo:Bar
~Foo:Bar
Foo:Baz
~Foo:Baz
Run Code Online (Sandbox Code Playgroud)

所以我猜整件事情都不可靠.

c++ c++11

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

为什么是T?不是可空类型吗?

为什么下面的代码不起作用?

public T? Method<T>() 
{
    /*
     error CS0403: Cannot convert null to type parameter 'T' 
     because it could be a non-nullable value type.
     Consider using 'default(T)' instead.
    */
    return null;
}
Run Code Online (Sandbox Code Playgroud)

c#

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

在构造函数初始化列表中使用placement new的语法是什么

假设我有一堂课

class MyClass
int buf[10];
public:
MyClass(int i) {
    new (&buf) OtherClass(i); // How to move this to constructor initialize list?
}
Run Code Online (Sandbox Code Playgroud)

:不用后,只需将该行复制到该位置即可。

c++ constructor initialization

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

Expressjs 如何仅在一条路线上启用 CORS?

下面的代码不起作用

app.post('/blah', (req, res) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, HEAD');
    res.status(204).send();
});
Run Code Online (Sandbox Code Playgroud)

请注意,我不想为整个应用程序打开 CORS。

node.js express

5
推荐指数
2
解决办法
6285
查看次数

这对“使用无堆栈协程来说,只有顶层例程可以被挂起”是什么意思。

我从这里找到了那句话。起初我很惊讶,因为我相信使无栈协程几乎毫无用处(而C ++协程TS是无栈的)。所以我写了一个演示(在使用C ++协程TS的Visual Studio中):

#include<experimental/coroutine>
#include<iostream>
#include<thread>
#include<mutex>
#include<future>
#include<chrono>

using namespace std;
using namespace std::chrono;
using namespace std::experimental;

class AsyncQueue {
public:
    class Awaitable {
        friend AsyncQueue;
        AsyncQueue& mQueue;
        coroutine_handle<> mCoroutineHandle;
        Awaitable* mNext = nullptr;
    public:
        Awaitable(AsyncQueue& queue):mQueue(queue){}

        bool await_ready() const noexcept {
            return false;
        }

        bool await_suspend(coroutine_handle<> coroutineHandle) noexcept
        {
            mCoroutineHandle = coroutineHandle;
            mQueue.enqueue(this);
            return true;
        }

        void await_resume() noexcept {}
    };
private:
    mutex mMutex;
    Awaitable* mHead = nullptr;
    Awaitable* mTail = nullptr;
    void enqueue(Awaitable* awaitable){ …
Run Code Online (Sandbox Code Playgroud)

c++ coroutine c++20 c++-coroutine

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

如何就地扩展分配的内存

std::realloc如果它无法扩展现有内存,是否有类似但什么都不做的事情(当然它会让你知道它失败了)?

我的意思是真正扩展到位。不复制到新创建的内存块。

c c++ memory-management c++11

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