easy_plot_cpp
easy_plot_cpp copied to clipboard
В функцию с переменным числом аргументов можно передавать только POD-типы
В коде есть функция
int plot(const std::string &name, const WindowSpec &wstyle, const size_t count, ...){
}
В С++11 и компиляторе gcc работать будет. Но до С++11 в функцию с переменным числом аргументов можно передавать только POD-типы. Нужно переделать
Пример кода
#include <cstdlib>
#include <vector>
#include <iostream>
struct LS {
double const r = 0, g = 1, b = 1, a = 1;
};
void fun() {
}
template <typename D, typename LS, typename... T>
void fun(std::vector<D> const &v, LS const &ls, T &&... t) {
std::cout << "vector: ";
for (auto const &x: v) {
std::cout << x << ' ';
}
std::cout << std::endl;
std::cout << "ls: " << ls.r << ", " << ls.g << ", " << ls.b << ", " << ls.a << std::endl;
fun(std::forward<T>(t)...);
}
int main() {
std::vector<double> const v1{1}, v2{2, 3}, v3{4, 5, 6};
fun(v1, LS{1, 0, 0}, v2, LS{0, 1, 0}, v3, LS{0, 0, 1});
return EXIT_SUCCESS;
}
Еще пример
#include <cstdlib>
#include <utility>
#include <initializer_list>
#include <vector>
#include <iostream>
struct LS {
double const r = 0, g = 1, b = 1, a = 1;
};
template <typename T>
using Pair = std::pair<std::vector<T> const &, LS const &>;
template <typename T>
Pair<T> pair(std::vector<T> const &v, LS const &ls) {
return {v, ls};
}
template <typename T>
void fun(std::initializer_list<T> const &il) {
std::cout << "std::size(il) = " << std::size(il) << std::endl;
for (auto const &[x, y]: il) {
std::cout << "vector: ";
for (auto const &i: x) {
std::cout << i << ' ';
}
std::cout << std::endl;
std::cout << "ls: " << y.r << ", " << y.g << ", " << y.b << ", " << y.a << std::endl;
}
}
int main() {
std::vector<double> const v1{1}, v2{2, 3}, v3{4, 5, 6};
fun({pair(v1, LS{1, 0, 0}), pair(v2, LS{0, 1, 0}), pair(v3, LS{0, 0, 1})});
return EXIT_SUCCESS;
}