Skip to content

Instantly share code, notes, and snippets.

@irondoge
Created June 10, 2017 00:55
Show Gist options
  • Save irondoge/94c9ea5cde1de107bfae43c7c0464e8e to your computer and use it in GitHub Desktop.
Save irondoge/94c9ea5cde1de107bfae43c7c0464e8e to your computer and use it in GitHub Desktop.
C/C++ compound litterals
#include <stdio.h>
void dummy(float *blblbl)
{
int i;
for (i = 0; i < 2; i++)
printf("%f\n", blblbl[i]);
}
int main()
{
int i;
dummy((float[]){ 42, 69 });
int (*owi)[] = &(int[]){ 1, 2, 3, 4 };
for (i = 0; i < 4; i++)
printf("%d\n", (*owi)[i]);
return (0);
}
#include <iostream>
#include <utility>
template<typename T>
struct $ {
T& val;
constexpr $(T&& val) : val(val) {}
constexpr T *operator&() const { return (&this->val); }
constexpr operator T&() { return (this->val); }
};
void dummy(float *blblbl)
{
int i;
for (i = 0; i < 2; i++)
std::cout << blblbl[i] << std::endl;
}
int main(int, char**)
{
int i;
dummy($<float[]>({ 42, 69 }));
auto owi = &$<int[]>({ 1, 2, 3, 4 });
for (i = 0; i < 4; i++)
std::cout << (*owi)[i] << std::endl;
return (0);
}
$ gcc ccompounds.c -W -Wall -Wextra -std=c89
$ ./a.out
42.000000
69.000000
1
2
3
4
$ g++ cppcompounds.cpp -W -Wall -Wextra -pedantic -std=c++14
$ ./a.out
42
69
1
2
3
4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment