小编JeJ*_*eJo的帖子

如何创建一个同时接受函数指针和 lambda 作为参数的函数?

我有以下类,该类有一个名为的方法errorHandler,需要使用多个不同的回调:

class IOPin;
class IOPinHandler
{
    IOPinHandler();
    virtual ~IOPinHandler();

    static bool ptrFun(IOPinHandler&) { return true; };

    template<typename T>
    bool init(IOPin& obj, const T& param);

    template<typename HandlerReturn = void, typename ...Args>
    HandlerReturn errorHandler(const GpioStatusCode& code, HandlerReturn(*callback)(IOPinHandler& obj, const Args&...), const Args&... args);

    // Added this overload to support passing lambdas as arguments, 
    // however does not seems to be called
    template<typename HandlerReturn = void, typename ...Args>
    HandlerReturn errorHandler(const GpioStatusCode& code, const std::function<HandlerReturn(IOPinHandler&, const Args&...)>& callback, Args&... args);
};
Run Code Online (Sandbox Code Playgroud)

我可以使用以下内容: …

c++ templates function-pointers function-templates c++11

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

如何为专用模板类提供更紧凑的定义?

考虑以下场景:

template <
    typename T,
    bool B = std::is_default_constructible_v<T>>
class toy_example;

template<typename T>
class toy_example<T, true>
{
public:
    toy_example() = default; // e.g. for default constructible types
    toy_example(const T& value);

public:
    void monomorphic(T);

private:
    T m_value;
};

template<typename T>
class toy_example<T, false> 
{
public:
    toy_example() = delete; // e.g. for non-default constructible types
    toy_example(const T& value); // Repeated declaration

public:
    void monomorphic(T); // Repeated declaration

private:
    T m_value;
};

// Implementation
template<typename T>
toy_example<T, true>::toy_example(const T& value) : m_value(value) {} …
Run Code Online (Sandbox Code Playgroud)

c++ templates sfinae class-template c++17

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

将清除共享智能指针上的呼叫重置

我最近改变了我的原始指针,std::shared_ptr在这样的容器中使用:

std::vector<std::shared_ptr<AbstractPathContainer>> mGeneratedPaths;
Run Code Online (Sandbox Code Playgroud)

当我打电话clear()给这个容器时,它会reset()在每个容器上调用方法std::shared_ptr吗?

c++ smart-pointers reset shared-ptr stdvector

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

如何简化 std::variant 类类型

我确信有一种简单的方法可以做到这一点,但在 SO 中找不到任何东西。在en.cppreference.com 中也找不到太多信息。

有没有办法简化 ,std::variant</*class types*/>以便我们可以声明可以std::variant作为参数的函数和类。

考虑这个例子:

我有一个向量,它充当以下内容的容器std::variant

std::vector<std::variant<Car, Space, Human>> EntityContainer;
Run Code Online (Sandbox Code Playgroud)

如果我想将此向量作为参数传递给函数,则必须添加以下参数声明。

void Function(std::vector <std::variant<Car, Space, Human>>& container);
Run Code Online (Sandbox Code Playgroud)

我也许可以在这个例子中使用,但这并不能真正解决问题。

有没有更好的解决方案,而不是std::variant在项目的任何地方一遍又一遍地列出相同的类类型?

代码直播

#include <iostream>
#include <vector>
#include <variant>


class Human
{
public:
  void Talk(){ std::cout << "whass up\n"; }
};
class Car
{
public:
  void RunEngine() { std::cout << "Vroom\n"; }
};
class Space
{
public:
  void Expand() { std::cout << "Expand slowly\n"; }
};

struct VisitPackage …
Run Code Online (Sandbox Code Playgroud)

c++ variant c++17 std-variant

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

无法创建抽象类的实例

当我尝试创建此类的实例时,我遇到此代码的问题,似乎出现错误

not allowed to use the abstracted class "SavingAccount"
Run Code Online (Sandbox Code Playgroud)

我不知道我能做什么.我按照edx Microsoft Intermediate C++给出的步骤进行操作.

BankAccount.h

#pragma once
#include <string>
class BankAccount
{
protected: 
    double balance;
public:

BankAccount(double initialBanlance);
virtual ~BankAccount();

double getBalance() const;

virtual void deposit(double amount);
virtual void withdraw(double amount);

virtual std::string getTermAndConditions() = 0;
virtual double getGuaranteeLimit() = 0;};
Run Code Online (Sandbox Code Playgroud)

BankAccount.cpp

#include "BankAccount.h"

BankAccount::BankAccount(double initialBanlance)
:balance(initialBanlance)
{}

BankAccount::~BankAccount()
{}

double BankAccount::getBalance() const
{return balance;}

void BankAccount::deposit(double amount)
{
balance += amount;
}

void BankAccount::withdraw(double amount)
{balance -= amount;}
Run Code Online (Sandbox Code Playgroud)

Freezable.h

#pragma …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance class c++11

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

如何分配二维向量?

我在一个问题中使用,但到目前为止dfs我还没有调用主函数,我的程序崩溃了。dfs最近我在编程c,现在我切换到cpp. 所以我是新手cpp

我知道我在向量中哪里出错了,请告诉我可以改进什么。我知道矢量可以自动增加其大小。

#include<iostream>
#include<vector>
using namespace std;
const int MAX = 100000;

bool visited[MAX] = { 0 };
int intime[MAX];
int outtime[MAX];

int timer = 0;
void dfs(vector<vector<int>> graph, int v)
{
    visited[v] = true;
    timer++;
    intime[v] = timer;
    vector<int>::iterator it = graph[v].begin();
    while (it != graph[v].end()) {
        if (visited[*it] == false)
        {
            dfs(graph, *it);
        }
        it++;
    }
    ++timer;
    outtime[v] = timer;
}

int main()
{
    vector<vector<int>> graph;
    graph[1].push_back(2); …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm stdvector data-structures c++11

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

一旦存在std :: underlying_type_t,“枚举类”相对于简单的“枚举”有什么优势?

enum class旨在提供类型安全性,从而通过整数提升消除了隐式转换为整数的方法,但是一旦获得std::underlying_type_t优势,enum class我就不清楚。

c++ enums c++14

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

在 C++ 中使用嵌套 if 语句与逻辑运算符 &amp;&amp;

假设我有一个整数数组表示棋盘上的棋子;

int board[8][8];
Run Code Online (Sandbox Code Playgroud)

在我的国际象棋游戏中,我目前正在编写一个生成器函数,它将返回所有合法移动的整数向量

当然,我将使用if 语句

我现在需要检查棋盘上的某个元素对于棋盘上的一块

例如,如果我有一个棋子;

board[row][col] == 'p';
Run Code Online (Sandbox Code Playgroud)

我需要生成[row+1][col][row+2][col]并且在某些情况下,如果它可以攻击一块,那么列中也会发生变化。

但是如果一块位于 的任何边缘boardboard[row+1][col]将返回索引超出范围

出于这个原因,我需要一个额外的 if 语句。

我的问题是,我应该使用:

if (pieceisnotonedge && board[row+1][col] == 0)
Run Code Online (Sandbox Code Playgroud)

或者

if (pieceisnotonedge)
{
    if (board[row+1][col] == 0)
}
Run Code Online (Sandbox Code Playgroud)

对于第一个示例,如果pieceisnotonedge返回false,它还会检查下一个条件吗?因为如果是这样,那我就有麻烦了。

c++ algorithm logic if-statement logical-operators

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

模板化向下转换到派生类并调用相应的方法

我在 main.cpp 中有一个例程,用户将在其中指定要在程序中执行的模式。一旦指定了模式,就会执行相应的块——首先将父求解器向下转换为子求解器,并调用solve子类中的关联方法。

    std::unique_ptr<SudokuSolver> solver;
    if (mode == MODES::SEQUENTIAL_BACKTRACKING) 
    {
        solver = std::make_unique<SudokuSolver_SequentialBacktracking>();
        SudokuSolver_SequentialBacktracking* child_solver = dynamic_cast<SudokuSolver_SequentialBacktracking*>(solver.get());
        child_solver->solve(board);
    } 
    else if (mode == MODES::SEQUENTIAL_BRUTEFORCE)
    {
        solver = std::make_unique<SudokuSolver_SequentialBruteForce>();
        SudokuSolver_SequentialBruteForce* child_solver = dynamic_cast<SudokuSolver_SequentialBruteForce*>(solver.get());
        child_solver->solve(board);
    }   
    else if (mode == MODES::PARALLEL_BRUTEFORCE)
    {
        int NUM_THREADS = (argc >= 5) ? std::stoi(argv[4]) : 2;
        omp_set_num_threads(NUM_THREADS);
        
        #pragma omp parallel
        {
            #pragma omp single nowait
            {
                solver = std::make_unique<SudokuSolver_ParallelBruteForce>();
                SudokuSolver_ParallelBruteForce* child_solver = dynamic_cast<SudokuSolver_ParallelBruteForce*>(solver.get());
                child_solver->solve(board);
            }
        }
    }
    else if (mode == MODES::SEQUENTIAL_DANCINGLINKS)
    {
        solver …
Run Code Online (Sandbox Code Playgroud)

c++ templates smart-pointers c++17

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

通过 const 限定对象调用成员函数会出现错误,因为函数未标记为 const?

代码

#include <iostream>
class A
{
public:
    mutable int x;
    mutable int y;

    A(int k1 = 0, int k2 = 0) :x(k1), y(k2) {}

    void display()
    {
        std::cout << x << "," << y << "\n";
    }
};

int main()
{
    const A a1;
    a1.x = 3;
    a1.y = 8;
    a1.display();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出

Error: 'this' argument to member function 'display' 
        has type 'const A', but function is not marked const
Run Code Online (Sandbox Code Playgroud)

我只是A::display()通过const限定对象调用成员函数a1。那么为什么 line …

c++ class constants mutable member-functions

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