任何人都可以解释为什么以下代码无法编译:
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) 以下代码无法编译:
#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也失败了.
我想编写一个类型特征,给定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++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?如果是,有哪些差异以及在哪些场景下推荐使用哪种版本的声明?
我想将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),这意味着替换失败,但我不明白为什么。我是否因为模式匹配而失去了可变性?
我想为类似元组的类型创建一个概念。类似元组的类型将是类似于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为任何索引。
为什么当共享指针超出范围时,此代码不会生成双重释放?
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) 我正在编写高度通用的代码,我正在将函数调用的返回值作为const auto&.例如:
const auto& value = foo();
Run Code Online (Sandbox Code Playgroud)
目的是以尽可能最通用的方式编写它,这样它就不会对返回类型做出任何假设foo,它可以通过值返回或通过引用返回而不会破坏客户端代码上的任何内容,即使该值是临时的.
但这段代码的表现最差:
const auto value = foo();
Run Code Online (Sandbox Code Playgroud)
如果foo()返回一个基本类型,如int或double或enum?
我正在编写通用代码,并且需要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。编译器说:“没有名为成员T中A”。
如何T以通用方式引用的构造函数?
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++ ×9
c++11 ×2
return-value ×2
shared-ptr ×2
templates ×2
auto ×1
c++-concepts ×1
c++20 ×1
const ×1
constants ×1
constexpr ×1
containers ×1
double-free ×1
googlemock ×1
iterator ×1
mutability ×1
overriding ×1
reference ×1
rust ×1
static ×1
tuples ×1
type-traits ×1