小编nya*_*108的帖子

模板参数扣除顺序

任何人都可以解释为什么以下代码无法编译:

template <typename P>
struct Base
{
    friend typename P::One;
    friend typename P::Two;
    friend typename P::Three;
};

template<typename U, typename D, typename T>
struct Derived : public Base<Derived<U,D,T>>
{
    using One   = U;
    using Two   = D;
    using Three = T;
};
Run Code Online (Sandbox Code Playgroud)

错误是:

..\PRP\main.cpp:3:1: error: no type named 'One' in 'struct Derived<A, B, C>'
 {
 ^
..\PRP\main.cpp:3:1: error: no type named 'Two' in 'struct Derived<A, B, C>'
..\PRP\main.cpp:3:1: error: no type named 'Three' in 'struct Derived<A, B, C>'
Run Code Online (Sandbox Code Playgroud)

为什么以下代码编译完美:

template …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11

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

从共享指针转换为指向const的共享指针

以下代码无法编译:

#include <iostream>
#include <memory>

class A
{
public:
    A( )
        : m_i( new int )
    { }

    std::shared_ptr< const int >&
    get( )
    {
        return m_i; // <-- invalid initialization of reference of type
                    //     'std::shared_ptr<const int>&' from 
                    //     expression of type 'std::shared_ptr<int>'
    }

private:
    std::shared_ptr< int > m_i;
};


int main( )
{
    A a;
    auto& i = a.get( );

    std::cout << *i << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如何从共享指针转换为常量对象的共享指针?static_cast也失败了.

c++ const return-value shared-ptr reference-binding

3
推荐指数
2
解决办法
514
查看次数

如何推导出最嵌套的迭代器类型?

我想编写一个类型特征,给定a ContainerType,能够推导出最嵌套的特征IteratorType,这意味着例如a std::vector<int>或a std::vector<std::vector<int>>或者std::vector<std::vector<std::vector<int>>>总是相同的,IteratorType将被推导出来,就好像它是一个std::vector<int>.

c++ containers iterator type-traits c++11

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

静态 constexpr 全局变量

在 C++17 中,像这样声明全局常量有什么区别:

namespace ns
{
static constexpr const auto global_variable = 47;
}
Run Code Online (Sandbox Code Playgroud)

还指定const修饰符,并且:

namespace ns
{
static constexpr auto global_variable = 47;
}
Run Code Online (Sandbox Code Playgroud)

没有指定const?如果是,有哪些差异以及在哪些场景下推荐使用哪种版本的声明?

c++ static constants global-variables constexpr

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

为什么 Option&lt;i32&gt; 在匹配模式中失去可变性?

我想将my_id,仅当存在时更改为另一个值:

fn read(id: &mut i32) {
    *id = 42;
}

struct S {
    my_id: Option<i32>,
}

impl S {
    fn run(&mut self) {
        match self.my_id {
            Some(mut id) => read(&mut id),
            _ => (),
        }
    }
}

fn main() {
    let mut s = S { my_id: 0.into() };

    s.run();

    println!("{:?}", s.my_id);
}
Run Code Online (Sandbox Code Playgroud)

操场

此代码打印Some(0),这意味着替换失败,但我不明白为什么。我是否因为模式匹配而失去了可变性?

pattern-matching mutability rust

3
推荐指数
2
解决办法
81
查看次数

C++20 概念检查类似元组的类型

我想为类似元组的类型创建一个概念。类似元组的类型将是类似于std::pair,std::tuple和 的东西std::array,提供编译时已知数量的类型,可通过编译时索引get<size>函数访问。

我是新概念,我不知道从哪里开始。不幸的是,STL 在<concepts>头文件中似乎没有这样的概念。

例如,我可以写:

template<typename T>
concept tuple_like = requires(T value)
{
    std::invoke(get<0>, value);
};
Run Code Online (Sandbox Code Playgroud)

但我不确定如何将其概括0为任何索引。

c++ c++-concepts c++20

3
推荐指数
2
解决办法
179
查看次数

共享指针:为什么没有双免费?

为什么当共享指针超出范围时,此代码不会生成双重释放?

int main()
{
    {
        auto * ptr = new int(1);
        shared_ptr<int> a( ptr );
        shared_ptr<int> b( ptr );
        cout << "ok: " << *a << *b << endl;
    }
    cout << "still ok" << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ shared-ptr double-free

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

可以在引用中存储返回值表现最差吗?

我正在编写高度通用的代码,我正在将函数调用的返回值作为const auto&.例如:

const auto& value = foo();
Run Code Online (Sandbox Code Playgroud)

目的是以尽可能最通用的方式编写它,这样它就不会对返回类型做出任何假设foo,它可以通过值返回或通过引用返回而不会破坏客户端代码上的任何内容,即使该值是临时的.

但这段代码的表现最差:

const auto value = foo();
Run Code Online (Sandbox Code Playgroud)

如果foo()返回一个基本类型,如intdoubleenum

c++ reference return-value auto

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

std ::应用于模板化函数中的构造函数

我正在编写通用代码,并且需要T使用通用可变参数元组调用通用模板参数的构造函数:

T& init_and_return(ArgsTuple& args)
{
    m_data = std::apply(&T::T, args); // here compiler complains
    return m_data;
}
Run Code Online (Sandbox Code Playgroud)

在我的主体中,T将有一个叫做的类型A。编译器说:“没有名为成员TA”。

如何T以通用方式引用的构造函数?

c++ templates tuples

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

谷歌模拟和覆盖关键字

Google Mock 中是否有宏f()通过将override关键字附加到宏替换来确保编译时检查签名:

struct I
{
    virtual void f() = 0;
};

struct MockI
{
    MOCK_METHOD0(f, void()); // this will define another function if f signature changes 
                             // leading to weird runtime test failures
};
Run Code Online (Sandbox Code Playgroud)

c++ overriding googlemock

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