Skip to content

Instantly share code, notes, and snippets.

@loliGothicK
Last active May 28, 2020 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loliGothicK/1e7ea7b2340d38dc5f00 to your computer and use it in GitHub Desktop.
Save loliGothicK/1e7ea7b2340d38dc5f00 to your computer and use it in GitHub Desktop.
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
}
using namespace std ;
#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を呼び出す
}
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
}
}
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
}
}
#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;
}
namespace ns = very_very_long_namespace;
namespace hoge = a::b::c::hoge;
using std::cout ; // std::cout を cout と書ける
using std::endl ; // std::endl を endl と書ける
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);
// ...
}
std::vector<int> v;
auto first = begin(v);
int arr[10];
begin(arr); // error
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