据我所知,默认情况下,该cherry-pick
命令接受提交并将其置于当前分支之上.是否有可能cherry-pick
在Git中提交并将其置于当前提交之下?
我试图使用奇怪的重复模板模式实现静态多态性,当我注意到static_cast<>
,通常在编译时检查一个类型是否实际可以转换为另一个,在基类声明中错过了一个错误,允许代码向下转换基础与其中一个兄弟姐妹同课:
#include <iostream>
using namespace std;
template< typename T >
struct CRTP
{
void do_it( )
{
static_cast< T& >( *this ).execute( );
}
};
struct A : CRTP< A >
{
void execute( )
{
cout << "A" << endl;
}
};
struct B : CRTP< B >
{
void execute( )
{
cout << "B" << endl;
}
};
struct C : CRTP< A > // it should be CRTP< C >, but typo …
Run Code Online (Sandbox Code Playgroud) 当人们已经知道某些需要动态多态性的代码中涉及的所有有限数量的类型时,与 usingenum
相比,using 的性能会更好,Box
因为后者使用动态内存分配,并且您需要使用具有虚拟函数调用的特征对象出色地。
也就是说,与使用std::variant
and的 C++ 中的等效代码相比std::visit
,在这种情况下,Rust 看起来涉及更多样板代码,至少对我来说是这样(我还没有学会使用过程宏)。在这里举一些例子:我有很多struct
类型:
struct A {
// ...
}
struct B {
// ...
}
// ...
struct Z {
// ...
}
Run Code Online (Sandbox Code Playgroud)
他们都实现了AlphabetLetter
以下特征:
trait AlphabetLetter {
fn some_function(&self);
}
Run Code Online (Sandbox Code Playgroud)
由于涉及的类型集是已知且有限的,我想使用enum
:
enum Letter {
AVariant(A),
BVariant(B),
// ...
ZVariant(Z),
}
Run Code Online (Sandbox Code Playgroud)
这里我们已经有了第一个样板:我需要为enum
涉及的每个类型变体的值添加一个名称。但真正的问题是:enum
Letter
它本身就是一个AlphabetLetter
,它只是代表了这样一个事实:我们在运行时不知道它是哪个字母。所以我开始为它实现这个特质:
impl AlphabetLetter for Letter {
fn some_function(&self) {
match self {
Letter::AVariant(letter) => letter.some_function(); …
Run Code Online (Sandbox Code Playgroud) 当定义泛型时, Ruststruct
有没有办法根据给定泛型类型实现的特征来使用方法的不同实现?T
例如:
struct S<T> {
value: T,
}
impl<T> S<T> {
fn print_me(&self) {
println!("I cannot be printed");
}
}
impl<T: std::fmt::Display> S<T> {
fn print_me(&self) {
println!("{}", self.value);
}
}
fn main() {
let s = S { value: 2 };
s.print_me();
}
Run Code Online (Sandbox Code Playgroud) 将类的const正确性扩展到其尖头成员的正确方法是什么?在示例代码中,get方法的常量版本是否会创建一个std::shared_ptr
其引用计数器与内部成员相同m_b
,或者是否重新开始计数0
?
class A
{
std::shared_ptr< B > m_b;
public:
std::shared_ptr< const B > get_b( ) const
{
return m_b;
}
std::shared_ptr< B > get_b( )
{
return m_b;
}
}
Run Code Online (Sandbox Code Playgroud) 我试图实现一个enum class
行为类似于C++ 11(具有类型安全性等)引入的行为,但它也表现为一个真正的类(使用构造函数,方法等).为了做到这一点,我保持内部enum
匿名:这有副作用,为了保持m_value
作为private
成员变量,我不得不添加一个static
名为的成员变量_
,如下所示:
#include <iostream>
#include <experimental/string_view>
class State
{
public:
static enum
{
UNKNOWN,
STARTED,
STOPPED
} _;
private:
using Type = decltype( _ );
Type m_value;
public:
constexpr State( Type value = UNKNOWN )
: m_value( value )
{ }
constexpr bool operator==( Type value ) const
{
return m_value == value;
}
constexpr std::experimental::string_view to_string( ) const
{
switch ( m_value )
{ …
Run Code Online (Sandbox Code Playgroud) 当我为我的项目编写代码时,我发现了一个非常奇怪的情况,即我的C++ 11代码无法在GCC 7.3和MSVC 2015之间进行交叉编译.案件基本上是这样的:
// .H file
template <typename X>
struct Outer {
X x = {};
template <typename Y>
struct Inner {
Y y = {};
Inner& operator++();
};
};
// .INL file
template <typename X>
template <typename Y>
inline Outer<X>::Inner<Y>&
Outer<X>::Inner<Y>::operator++()
{
++y;
return *this;
}
// .CPP file
#include <iostream>
int main()
{
Outer<int>::Inner<int> a;
++a;
std::cout << a.y << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
海湾合作委员会不会抱怨上述代码.但MSVC会,错误将是:
warning C4346: 'Inner': dependent name is …
Run Code Online (Sandbox Code Playgroud) 我没想到要编译这段代码:
#include <iostream>
#include <memory>
class A
{
public:
inline int get() const
{
return m_i;
}
inline void set(const int & i)
{
m_i = i;
}
private:
int m_i;
};
int main()
{
const auto ptr = std::make_unique< A >();
ptr->set( 666 ); // I do not like this line D:<
std::cout << ptr->get( ) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果ptr是一个原始C指针,我会好的.但由于我使用的是智能指针,我无法理解这背后的基本原理是什么.
我使用唯一指针来表达所有权,在面向对象编程中,这可以被视为对象组合("部分关系").
例如:
class Car
{
/** Engine built through some creational …
Run Code Online (Sandbox Code Playgroud) c++ pointers smart-pointers const-correctness object-composition
为了减少不必要的耦合并减少编译时间,是否有一个工具可以帮助定位(或可能以交互方式删除)C / C ++转换单元中不必要的头文件包含?
尤其是,该工具可以针对每个翻译单元#include
在该翻译单元的上下文中检测其指令可以用正向声明替换的所有那些类型。
如果无法实现,是否存在有用的近似值?
我有一个使用返回码的类:
class MyClass
{
// ...
public:
// ValueType get_value() const; // usual code
ErrorCode get_value(ValueType& value) const; // uses error code
// ...
};
Run Code Online (Sandbox Code Playgroud)
因此,第二种形式get_value()
实际上将值作为函数参数提供,而不是作为返回值.
是否有可能推断出函数参数的类型get_value()
,也许使用 decltype
?
int main()
{
// ...
MyClass my_class;
// auto val = my_class.get_value(); // okay: value has correct type
declytype( /* something here */ ) value;
const auto error = my_class.get_value( value );
// ...
}
Run Code Online (Sandbox Code Playgroud)