2 pytania:
Question 1: Jak wywolac w ANSI C taki program w C++:
#include <iostream>
#include <string>
#include <limits>
using std::string;
using std::cerr;
#define LOG_FUNC(x) cerr << '\n' << #x " = " << x << '\t'
#define LOG_ARG(x) cerr << #x " = " << x << '\t'
template <typename T>
class Money {
static_assert((std::numeric_limits<T>::is_integer || std::is_floating_point<T>::value) && "Number required.");
public:
Money(const string & dollars, const long double cents);
Money(const string & dollars);
static Money create(const string & dollars, const long double cents);
static Money create(const string & dollars);
};
template <typename T>
Money<T>::Money(const string & dollars, const long double cent) {
LOG_FUNC(__func__);
LOG_ARG(dollars);
LOG_ARG(cent);
}
template <typename T>
Money<T>::Money(const string & dollars) {
LOG_FUNC(__func__);
LOG_ARG(dollars);
}
template <typename T>
Money<T> Money<T>::create(const string & dollars) {
LOG_FUNC(__func__);
LOG_ARG(dollars);
return Money<T>(dollars);
}
template <typename T>
Money<T> Money<T>::create(const string & dollars, const long double cents) {
LOG_FUNC(__func__);
LOG_ARG(dollars);
LOG_ARG(cents);
return Money<T>(dollars, cents);
}
template <typename T>
struct Creation {
template<typename... Args>
Money<T> operator()(Args...args) const {
return Money<T>::create(std::forward<Args>(args)...);
}
};
template <class Type, template<typename> class Template>
struct Constructor {
template<typename... Args>
Template<Type> operator()(Args...args) const {
return Template<Type>(std::forward<Args>(args)...);
}
};
template <typename Function, typename... Args>
void call(Function && f, Args &&... args ) {
f(std::forward<Args>(args)...);
}
int main() {
call(Constructor<int, Money>(), "-20.8", 5.0L);
call(Constructor<long long, Money>(), "78");
call(Creation<unsigned long long>(), "2", 5);
call(Creation<long double>(), "7");
cerr << '\n';
}
// g++ -Wfatal-errors -Wall -Wextra -Wconversion -std=c++14
probowalem z extern "C" i void*, ale poza recznym przepisywaniem kazdej klasy szablonowej czyli: Money<int>, Money<long double="double">, ...,
wszystko co spelnia warunek: static_assert((std::numeric_limits<T>::is_integer || std::is_floating_point<T>::value) && "Number required.");
to nie mam pomyslu.
Question 2: A i czy ten program w samym C++ (bez odwolywania do C) to tez mozna jakos lepiej napisac bo nie wydaje mi sie abym wyczerpal najlepsze mozliwosci