( C++20)
cppreference.com
(TU-local - Translation unit local) , ( ), .
C++: 2 :
// TU-local
export module Foo;
import <iostream>;
namespace {
class LolWatchThis { // ,
static void say_hello() {
std::cout << " \n";
}
};
}
export LolWatchThis lolwut() { // LolWatchThis
return LolWatchThis();
}
// main.cpp
import Foo;
int main() {
auto evil = lolwut(); // 'evil' 'LolWatchThis'
decltype(evil)::say_hello(); // 'LolWatchThis'
//
}
TU-
TU-,
- , , ,
- ,
- -, TU- ,
- , , ( , ), TU- ,
- TU- ,
- TU-
- , (, ) ( ).
// TU-
namespace { // , ,
//
int tul_var = 1; // TU-
int tul_func() { return 1; } // TU-
struct tul_type { int mem; }; // TU- ()
}
template<typename T>
static int tul_func_temp() { return 1; } // TU-
// TU-
template<>
static int tul_func_temp<int>() { return 3; } // TU-
// TU-
template <> struct std::hash<tul_type> { // TU-
std::size_t operator()(const tul_type& t) const { return 4u; }
};
| : #1.2, #2 #5 |
TU-,
static int tul_var = 1; // TU-
static int tul_func() { return 1; } // TU-
int* tul_var_ptr = &tul_var; // TU-: TU-
int (* tul_func_ptr)() = &tul_func; // TU-: TU-
constexpr static int tul_const = 1; // TU- ,
//
int tul_arr[] = { tul_const }; // TU-: TU- constexpr
struct tul_class { int mem; };
tul_class tul_obj{tul_const}; // TU-: constexpr TU-
//
D E,
- D - E,
- E , D -, -, --, - -, E,
- E , D , E -, , E.
//
auto x = [] {}; // decltype(x)
// ()
int y1 = 1; // y1 (-)
struct y2 { int mem; };
y2 y2_obj{1}; // y2 (-)
struct y3 { int mem_func(); };
int y3::mem_func() { return 0; } // y3 (--)
template<typename T> int y4 = 1;
int var = y4<y2>; // y4 (-)
template<typename T> concept y5 = true;
template<typename T> void func(T&&) requires y5<T>; // y5 (-)
// ()
int z1(int arg) { std::cout << " "; return 0; }
int z2(int arg) { std::cout << " 1"; return 1; }
int z2(double arg) { std::cout << " 2"; return 2; }
int val1 = z1(0); // z1
int val2 = z2(0); // z2 ( int z2(int) )
, TU- ,
- ( (, ) , ),
- ( ),
- -volatile , , odr,
constexpr, TU- .
| : |
TU-
, , TU- , , , . , , .
| : |
#1:
export module A;
static void f() {}
inline void it() { f(); } // : f
static inline void its() { f(); } // OK
template<int> void g() { its(); } // OK
template void g<0>();
decltype(f) *fp; // : f ( )
// TU-
auto &fr = f; // OK
constexpr auto &fr2 = fr; // : f
constexpr static auto fp2 = fr; // OK
struct S { void (&ref)(); } s{f}; // OK: TU-
constexpr extern struct W { S &s; } wrap{s}; // OK: TU-
static auto x = []{f();}; // OK
auto x2 = x; // : TU-
int y = ([]{f();}(),0); // : TU-
int y2 = (x,0); // OK
namespace N {
struct A {};
void adl(A);
static void adl(int);
}
void adl(double);
inline void h(auto x) { adl(x); } // ,
#2:
module A;
void other() {
g<0>(); // OK:
g<1>(); // : TU-local
h(N::A{}); // : TU- N::adl(int)
h(0); // OK: adl(double)
adl(N::A{}); // OK; N::adl(int) , N::adl(N::A)
fr(); // OK: f
constexpr auto ptr = fr; // : fr
//
}
| : , |