Last active
May 28, 2020 09:15
-
-
Save loliGothicK/1e7ea7b2340d38dc5f00 to your computer and use it in GitHub Desktop.
namespaceの賢い使い方 ref: https://qiita.com/_EnumHack/items/430da105a541f9ecd774
This file contains 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
namespace A | |
{ | |
void f(){std::cout << "A" << std::endl;}; | |
void hoge(){ | |
f(); // 名前空間の中では修飾しなくても呼び出せる | |
} | |
} | |
namespace B | |
{ | |
void f(){std::cout << "B" << std::endl;}; | |
void hoge(){ | |
A::f(); // 別の名前空間から呼びたいなら修飾する | |
} | |
} | |
int main() | |
{ | |
A::f(); // A | |
B::f(); // B | |
A::hoge(); // A | |
B::hoge(); // A | |
} |
This file contains 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
using namespace std ; |
This file contains 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 <iostream> | |
namespace my_lib { | |
namespace v1 { | |
void f() | |
{ | |
std::cout << "v1" << std::endl; | |
} | |
} | |
inline namespace v2 { | |
void f() | |
{ | |
std::cout << "v2" << std::endl; | |
} | |
} | |
} | |
int main() | |
{ | |
my_lib::v1::f(); // 古いバージョンのAPIを呼び出す | |
my_lib::v2::f(); // バージョンを明示的に指定してAPIを呼び出す | |
my_lib::f(); // ライブラリデフォルトのAPIを呼び出す | |
} |
This file contains 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
namespace my_literals { | |
inline namespace integer_literals { | |
inline auto operator"" _kilo(unsigned long long x) | |
{ return int(x) * 1'000; } | |
} | |
inline namespace floating_point_literals { | |
inline auto operator"" _kilo(long double x) | |
{ return double(x) * 1'000; } | |
} | |
} | |
int main() { | |
{ | |
using namespace my_literals::integer_literals; // int用だけ可視化される | |
1_kilo; // 1000 | |
} | |
{ | |
using namespace my_literals::floating_point_literals; // double用だけ可視化される | |
1_kilo; // 1000.0 | |
} | |
{ | |
using namespace my_literals; // 全部可視化される | |
1_kilo; // 1000 | |
1.0_kilo; // 1000.0 | |
} | |
} |
This file contains 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
namespace my_literals { | |
inline namespace integer_literals { | |
inline auto operator"" _kilo(unsigned long long x) | |
{ return int(x) * 1'000; } | |
inline auto operator"" _mega(unsigned long long x) | |
{ return int(x) * 1'000'000; } | |
} | |
inline namespace floating_point_literals { | |
inline auto operator"" _kilo(long double x) | |
{ return double(x) * 1'000; } | |
inline auto operator"" _mega(long double x) | |
{ return double(x) * 1'000'000; } | |
} | |
} | |
int main() { | |
{ | |
using namespace my_literals::integer_literals; // int用だけ可視化される | |
1_kilo; // 1000 | |
} | |
{ | |
using namespace my_literals::floating_point_literals; // double用だけ可視化される | |
1_kilo; // 1000.0 | |
} | |
{ | |
using namespace my_literals; // 全部可視化される | |
1_kilo; // 1000 | |
1.0_kilo; // 1000.0 | |
} | |
} |
This file contains 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 <iostream> | |
using namespace std; | |
template < typename T > | |
T max(T const& a, T const& b) | |
{ | |
return a < b ? b : a ; | |
} | |
int main(){ | |
cout << max(1,2) << endl; // エラーだよ | |
return 0; | |
} |
This file contains 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
namespace ns = very_very_long_namespace; |
This file contains 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
namespace hoge = a::b::c::hoge; |
This file contains 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
using std::cout ; // std::cout を cout と書ける | |
using std::endl ; // std::endl を endl と書ける |
This file contains 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
template < class Range > | |
void f(const Range & range){ | |
using std::begin; // for ADL | |
using std::end; // for ADL | |
auto first = begin(range); | |
auto last = end(range); | |
// ... | |
} |
This file contains 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
std::vector<int> v; | |
auto first = begin(v); |
This file contains 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
int arr[10]; | |
begin(arr); // error |
This file contains 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
namespace my_space { | |
// 独自配列 | |
struct MyArray{ /* ... */}; | |
// MyArray専用begin | |
decltype(auto) begin(const MyArray& a) { return /* ... */; } | |
} | |
template < class Range > | |
void f(Range&& range){ | |
using std::begin; | |
auto first = begin(range); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment