Skip to content

Instantly share code, notes, and snippets.

@orontee
Created September 20, 2013 12:36
Show Gist options
  • Save orontee/6636816 to your computer and use it in GitHub Desktop.
Save orontee/6636816 to your computer and use it in GitHub Desktop.
#include <iostream>
const int LENGTH = 8;
template<typename T>
void
output_array(T *p)
{
for (int i = 0; i < LENGTH; ++i)
{
std::cout << *(p + i);
}
std::cout << "\n";
}
int TEST_CTOR_CALL = 0;
struct pod_test {
int data;
};
struct test {
operator char() const { return 'X'; }
test() { ++TEST_CTOR_CALL; }
test(int) {}
};
int main()
{
std::cout << "Omitting initializer results in uninitialized PODs\n";
char a0[LENGTH];
output_array<char>(a0);
pod_test a1[LENGTH];
std::cout << a1->data << (a1 + 1)->data << "...\n";
std::cout << "Leaving initializer empty initializes PODs to 0\n";
char a2[LENGTH] = {};
output_array<char>(a2);
int a3[LENGTH] = {};
output_array<int>(a3);
pod_test a4[LENGTH] = {};
std::cout << a4->data << (a4 + 1)->data << "...\n";
std::cout << "Omitting initializer cause default constructor of non-POD types to be called\n";
test a5[LENGTH];
output_array<test>(a5);
test a6[LENGTH] = { 5, };
output_array<test>(a6);
std::cout << "(test default constructor called: " << TEST_CTOR_CALL << ")\n";
std::cout << "Finally, using new\n";
char *a7 = new char[8];
output_array<char>(a7);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment