vector<A*>::erase
с необычными параметрами. Какие — в ошибке не говорится, вот все перегрузки.// Спрессовать без перевыделения памяти, получить новый конец
std::vector<A*>::iterator newEnd = std::remove(v.begin(), v.end(), whatToDelete);
// Удалить освободившийся хвост скопом!
v.erase(newEnd, v.end());
v.erase(v.begin() + indexToDelete);
void fillCombo(QComboBox* x) {} // хорошо
fillCombo(ui->comboHistory);
void fillCombo(QComboBox& x) {} // плохо
fillCombo(*ui->comboHistory);
// Хуже, но возможно
void import(AsyncRunner* asy) {
if (asy) {
asy->run([](){
doImport(x);
});
} else {
doImport(x);
}
}
import(nullptr);
// Лучше
void import(AsyncRunner& asy) {
asy.run([](){
doImport(x);
});
}
import(NoAsync::INST);
void write(size_t size, const char* data) {}
void save(std::string_view fileName) {}
void transform(Matrix4 x) {}
Obj::Obj(std::string aName) : name(std::move(aName)) {}
float time = clock.restart().asMicroseconds();
Сейчас компы быстры и погрешность незначительна, но всё-таки. Work* p = NULL;
if () {
p = new Work();
}
delete p;
std::unique_ptr<Work> p;
if () {
p = std::make_unique<Work>(); // простите, это Си++14, на 11 чуть не так.
}
CONFIG -= qt
TEMPLATE = lib
CONFIG += staticlib
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS MUPARSER_STATIC
QMAKE_CXXFLAGS += -Wno-deprecated-copy -Wno-cast-function-type
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
src/muParser.cpp \
src/muParserBase.cpp \
src/muParserBytecode.cpp \
src/muParserCallback.cpp \
src/muParserError.cpp \
src/muParserInt.cpp \
src/muParserTest.cpp \
src/muParserTokenReader.cpp
HEADERS +=
INCLUDEPATH += include
DEFINES += MUPARSER_STATIC
INCLUDEPATH += ../MuParser/include
LIBS += -L$$PWD -lmuparser
pacboy sync muparser:i muparser:x
#include <iostream>
class Vocal { // интерфейс
public:
virtual void shout() = 0;
virtual ~Vocal() = default;
};
class Dog : public Vocal {
public:
virtual void shout() override { std::cout << "Woof!" << std::endl; }
};
class Unrelated {};
// ВАРИАНТ 1. Метод выкручивания рук.
void shoutAll_v1(Vocal** x, int n)
{
for (int i = 0; i < n; ++i)
x[i]->shout();
}
template <class T, int N>
inline void shoutAll_v1(T* (&x)[N]) {
// Тупая проверка концепции, ждём Си++20
static_assert (std::is_base_of<Vocal, T>(), "Need array of Vocal");
T** xx = x;
shoutAll_v1(reinterpret_cast<Vocal**>(xx), N);
}
// ВАРИАНТ 2. Виртуальный полиморфизм.
class Choir { // интерфейс
public:
virtual int size() const = 0;
virtual Vocal& at(size_t i) const = 0;
virtual ~Choir() = default;
};
void shoutAll_v2(const Choir& x)
{
int sz = x.size();
for (int i = 0; i <sz; ++i)
x.at(i).shout();
}
template <class T>
class VocalArray : public Choir {
public:
template <int N>
VocalArray(T* (&x)[N]) : fData(x), fSize(N) {}
int size() const override { return fSize; }
Vocal& at(size_t i) const override { return *fData[i]; }
private:
T** const fData;
int fSize;
};
int main()
{
Dog* sons[3];
for(auto& x : sons) {
x = new Dog;
}
//Unrelated* unrel[3];
std::cout << "V1" << std::endl;
shoutAll_v1(sons);
//shoutAll_v1(unrel); не компилируется
std::cout << "V2" << std::endl;
shoutAll_v2(VocalArray<Dog>(sons));
//shoutAll_v2(VocalArray<Unrelated>(unrel)); не компилируется
return 0;
}