小编Pau*_*ill的帖子

tr1 :: shared_ptr和boost :: shared_ptr之间的区别?

tr1::shared_ptr和之间有什么区别boost::shared_ptr吗?如果是这样,什么?

c++ boost tr1 shared-ptr

13
推荐指数
1
解决办法
1991
查看次数

如何在没有任何或对象的情况下制作 TypeScript 类型保护?

我正在使用 typescript-eslint v3.1.0 并在 TypeScript 中具有以下类型保护功能:

interface JWTPayload {
    sub: number;
}

function isJWTPayload(obj: unknown): obj is JWTPayload {
    if (typeof obj !== 'object') {
        return false;
    }

    // eslint-disable-next-line @typescript-eslint/ban-types
    const obj2 = obj as object;

    if (!('sub' in obj2)) {
        return false;
    }

    const obj3 = obj2 as JWTPayload;

    if (!Number.isInteger(obj3.sub)) {
        return false;
    }

    return true;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是这样的:在我想象的常见场景中禁用 lint 规则感觉不太好。是否有 TypeScript 模式可以避免这种情况?

一些背景:

被禁用的 typescript-eslint 规则是在https://github.com/typescript-eslint/typescript-eslint/pull/848中引入的,其中认为“99.9%的时间,你不希望使用[“对象”类型],而绝大多数代码库都不想使用它”。他们为什么会这么说?似乎每当您验证用户输入时都会使用它。有没有其他方法可以在不强制转换为“any”的情况下做到这一点?

typescript eslint

9
推荐指数
1
解决办法
3068
查看次数

从C ++模板中的非const l值引用推导出const l值引用

假设您具有以下一对功能:

void f(const int&) { 
    // Do something, making a copy of the argument.
}

void f(int&&) { 
    // Do the same thing, but moving the argument.
}
Run Code Online (Sandbox Code Playgroud)

它们是相当多余的-函数之间的唯一区别是它们是复制还是移动参数。当然,通过将其重写为单个模板函数,我们可以做得更好:

template<typename T> 
void g(T&&) { 
    // Do something, possibly using std::forward to copy or move the argument.
}
Run Code Online (Sandbox Code Playgroud)

这有效,并且在实践中是常用的惯用语。但是该模板可能被实例化为三个函数,而上面的两个函数又是如此。我们可以使用以下代码来验证这种情况:

#include <iostream>

template<typename T> constexpr char *type = nullptr;
template<> constexpr const char *type<int&> = "int&";
template<> constexpr const char *type<const int&> = "const int&";
template<> constexpr const char *type<int> = "int"; …
Run Code Online (Sandbox Code Playgroud)

c++ templates perfect-forwarding c++11 template-argument-deduction

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