Last active
November 7, 2018 04:17
-
-
Save makotoshimazu/326b8c5ff12cf69274e1f18e702c8986 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "foo.h" | |
constexpr int Foo::kValue; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
class Foo { | |
public: | |
static constexpr int kValue = 10; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <array> | |
#include <iostream> | |
#include "foo.h" | |
constexpr int addOne(int x) { return x + 1; } | |
int main() { | |
// Foo::kValue is a constant value. | |
std::cout << "result: " << Foo::kValue << std::endl; | |
// That's a constant, so you can use it as a template parameter. | |
std::array<int, Foo::kValue> arr = {0}; | |
for (const auto& x : arr) | |
std::cout << x << " "; | |
std::cout << std::endl; | |
for (auto& x : arr) | |
++x; | |
for (const auto& x : arr) | |
std::cout << x << " "; | |
std::cout << std::endl; | |
// You can calculate values on compilation time if all values and functions | |
// are declared as constexpr. | |
std::array<int, addOne(Foo::kValue)> arr2; | |
std::cout << "arr2 size: " << arr2.size() << std::endl; | |
// You cannot retrieve the address if Foo::kValue doesn't have the | |
// storage. You need to have it at somewhere (like foo.cc). | |
std::cout << &Foo::kValue << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment