小编Usi*_*Cpp的帖子

如何通过扩展以下类型特征来删除decltype(&MyClass :: funct)部分?

我想拥有类型特征,这将有助于我从成员函数指针中获取类的类型。我调查了这个答案 ,发现我已经达到目标了。

看起来像这样:

#include <iostream>

// example class
struct MyClass {
    void funct() { std::cout << "funct has been called....\n"; }
};

// traits
template<typename Class> struct get_class{};
template<typename ReType, typename Class, typename... Args>
struct get_class<ReType(Class::*)(Args...)>
{
    using type = Class;
};
template<typename Type> using get_class_t = typename get_class<Type>::type;

int main()
{
    get_class_t<decltype(&MyClass::funct)> myObj;
    //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ---> this is a lot of typing
    myObj.funct();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,如代码中所示,我每次都需要编写,get_class_t<decltype(&MyClass::funct)> 或者

  auto ptr =  &MyClass::funct;
  get_class_t<decltype(ptr)> myObj;
  //         ^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

这很多decltype() …

c++ templates member-function-pointers type-traits c++11

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

如何有条件地实例化具有多个模板参数的模板类?

我关注了这篇文章:类模板SFINAE有条件地实例化模板类。

如上面的链接所示,这对于只有一个模板参数的类非常有效。

但是,我有两个(模板)参数,我想做一些SFINE检查。以下是我的代码的最小示例。

#include <type_traits>
#include <string>

template<class T, class U, class R> using arithmetic_types =  std::enable_if_t<
    std::is_arithmetic_v<T> &&
    std::is_arithmetic_v<U>,
    R
>;

template<class T, class U, class Enable = void> class MyClass;
template<class T, class U, arithmetic_types<T, U, void>> 
class MyClass {
public:
    MyClass() = default;
};

int main()
{
    MyClass<int, int> o;          // should work
    MyClass<int, double> o1;      // should work
    MyClass<int, std::string> o2; // should be a complier error
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面给了我错误信息:https …

c++ templates class sfinae c++17

6
推荐指数
1
解决办法
129
查看次数

我应该移动 std::exchange ed 成员吗?

std::exchange可用于impliment移动构造函数。这是来自 cppreference.com https://en.cppreference.com/w/cpp/utility/exchange#Notes的示例。

但是,可能的实现std::exchange如下所示:

template<class T, class U = T>
T exchange(T& obj, U&& new_value)
{
    T old_value = std::move(obj);
    obj = std::forward<U>(new_value);
    return old_value;          // can be copy (until C++17) or a move (C++17), right?
}
Run Code Online (Sandbox Code Playgroud)

现在我的情况:

#include <string>
#include <utility>

struct MyClass
{
    std::string m_str;
    // some other non-primitive members of the class

    MyClass(MyClass&& other) : m_str{ std::exchange(other.m_str, {}) } // enough?
        // or
        // : m_str{ std::move(std::exchange(other.m_str, {})) }
        //          ^^^^^^^^^^    do …
Run Code Online (Sandbox Code Playgroud)

c++ move-constructor c++14

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

对象的销毁是否正确进行?

我在这里阅读了这篇文章:什么时候使用虚拟析构函数? 我的想法是,每当我们使用动态new指针或智能指针动态创建对象时,基类都应具有适当的virtual析构函数,以在删除对象时对其进行破坏。

然后,我发现了一些如下代码(简化形式),它错过了virtual析构函数Base

class Base
{
public:
    // some static members
};

class Derived1 final : public Base
{
public:
    // other members
    // default constructor does not construct the `Base` in constructor member intilizer
    Derived1() {};
    virtual ~Derived1() = default;
};
class Derived2 final : public Base
{
public:
    // other members
    Derived2() {}; // default constructor does not construct the `Base`
    ~Derived2() = default;
};

int main()
{
    // …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism virtual class c++11

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

如何忽略元组的某些元素?

我有一个包含一些元素的元组,我想将元组的一些元素分配给变量,并忽略其中一些元素。

 auto tuple1 = std::make_tuple(1,2,3,4);
 // variable a should be placeholder 1 in tuple and variable b should be place holder 3;
 int a,b ;


 
Run Code Online (Sandbox Code Playgroud)

c++ tuples c++17

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