Skip to content

Instantly share code, notes, and snippets.

@jitpaul

jitpaul/consexpr Secret

Last active Nov 24, 2019
Embed
What would you like to do?
constexpr
template <int val>
class Something{
int member_a;
public:
Something():member_a(val){}
};
constexpr int func(int x, int y){
return x*y;
}
int main(){
int n1 = 5; // 'n1' is neither
// const or constexpr
int array1[n1]; // ERROR.
Something <n1> s1; // ERROR.
const int n2 = 6; // 'n2' is a
// const object.
int array2[n2]; // OK.
Something <n2> s2; // OK
constexpr int n3 = 7; // 'n3' is
// a constexpr object
int array3[n3]; // OK.
Something <n3> s3; // OK
int array4[func(5,6)]; // OK. 'func' is
// constexpr object
Something <func(5,6)> s4; // OK. 'func' is
// constexpr object
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment