ฉันกำลังค้นหาวิธีที่จะระบุ lambdas ที่ว่างเปล่า (ไม่มีขอบเขต) จาก lambdas อื่น ๆ ในฟังก์ชั่นเทมเพลต ปัจจุบันฉันใช้ C ++ 17 แต่ฉันอยากรู้คำตอบ C ++ 20 ด้วย
รหัสของฉันมีลักษณะเช่นนี้:
template<typename T>
auto func(T lambda) {
// The aguments of the lambdas are unknown
if constexpr (/* is captureless */) {
// do stuff
}
}
มันรับประกันโดยมาตรฐาน C ++ (17 หรือ 20) ที่แลมบ์ดาที่ไม่มีตัวแปลงซึ่งแปลงสภาพเป็นตัวชี้ฟังก์ชั่นจะทำให้ได้std::is_empty
ผลจริงหรือไม่?
ใช้รหัสนี้เป็นตัวอย่าง:
auto a = []{}; // captureless
auto b = [c = 'z']{}; // has captures
static_assert(sizeof(a) == sizeof(b)); // Both are the same size
static_assert(!std::is_empty_v<decltype(b)>); // It has a `c` member
static_assert(std::is_empty_v<decltype(a)>); // Passes. It is guaranteed?
+lambda
) นั้นมีรูปแบบที่ดีหรือไม่