copy()
まず、以下は、静的にチェックされた関数を作成する C++ 2011 ソリューションです。引数として s と schar
の配列を受け入れ、渡される最初の配列を満たすためにchar
正確に s の量を追加する必要があります。char
もちろん、静的チェックは必要に応じて削除できます。元のバージョンには、いくつかのタイプミスと脱落がありました。
#include <algorithm>
#include <iostream>
#include <iterator>
int constexpr size(char const&) { return 1; }
template <int Size>
int constexpr size(char const(&)[Size]) { return Size; }
template <typename T0, typename... T>
int constexpr size(T0 const& arg0, T const&... args) {
return size(arg0) + size(args...);
}
char* copy_intern(char* to, char c) { *to = c; return ++to; }
template <int Size>
char* copy_intern(char* to, char const (&array)[Size]) {
return std::copy(array, array + Size, to);
}
template <typename T0, typename... T>
char* copy_intern(char* to, T0 const& arg0, T const&... args) {
return copy_intern(copy_intern(to, arg0), args...);
}
template <int Total, typename... T>
void copy(char (&to)[Total], T const&... args)
{
static_assert(Total == size(args...), "wrong argument size");
copy_intern(to, args...);
}
int main()
{
char buff0 = 'a';
char buff1 = 'b';
char buff2[2] = { 'c', 'd' };
char buff3[4] = { 'e', 'f', 'g', 'h' };
char buff4[8] = { 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p' };
char final[16];
copy(final, buff0, buff1, buff2, buff3, buff4);
*std::copy(final, final + 16,
std::ostreambuf_iterator<char>(std::cout)) = '\n';
}
copy_intern()
最初の 2 つの関数を C++ 2003 でも使用して、引数の型とサイズを推測できることに注意してください。つまり、これらの関数を使用すると、適切と見なされるように名前が変更される可能性があり、少なくともサイズを自動的に取得するものを取得できます。
char* tmp = final;
tmp = copy_intern(tmp, buff0);
tmp = copy_intern(tmp, buff1);
tmp = copy_intern(tmp, buff2);
tmp = copy_intern(tmp, buff3);
tmp = copy_intern(tmp, buff4);