- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
#include <iostream>
#include <type_traits>
#include <list>
#include <vector>
using std::cout;
using std::endl;
using function = int;
struct Console {
private:
template<typename SS, typename TT>
static auto test(int)
-> decltype(std::declval<SS&>() << std::declval<TT>(), std::true_type());
template<typename, typename>
static auto test(...) -> std::false_type;
template<typename T>
static const bool canCout = decltype(test<decltype(cout), T>(0))::value;
public:
template<typename T>
typename std::enable_if<std::is_same<decltype(std::declval<T>().begin()),
decltype(std::declval<T>().end())>::value && !canCout<T>>::type
log(T arg) {
log('[');
for (typename T::const_iterator it = arg.begin(); it != arg.end(); ++it) {
auto nextIt = it;
++nextIt;
if (nextIt != arg.end()) {
log(*it);
log(", ");
} else {
log(*it);
log(']');
}
}
}
template<typename T>
typename std::enable_if<canCout<T>>::type
log(T arg) {
cout << arg;
}
template<typename H, typename ... T>
void log(H arg, T... rest) {
log(arg);
log(' ');
log(rest...);
}
};
static Console console;
function main()
{
console.log(std::vector<int>({ 1, 2, 3 }), "Hello World!", 100.1, "\n");
console.log(std::string("std::string"), std::list<std::string>({ "one", "two", "three" }), '\n');
return 0;
}
Javascript++.
https://ideone.com/NykL0u