c 如何在編譯期判斷乙個物件是否是字串字面值?

時間 2021-05-29 22:56:31

1樓:

,套路有點偏……

這個是簡化版本,轉義符和寬字元都沒有處理,實用版本留給讀者思考好了【滾

#include

#include

#define is_literal_(x) is_literal_f(#x, sizeof(#x) - 1)

#define is_literal(x) is_literal_(x)

constexpr

inline

bool

is_literal_f_se

(char

const*s

,std

::size_t

len)

constexpr

inline

bool

is_literal_f_s1

(char

const*s

,std

::size_t

len)

constexpr

inline

bool

is_literal_f

(char

const*s

,std

::size_t

len)

intmain()

2樓:Albert

使用使用者定義字面量

inline

String

operator""s

(char

const*s

,std

::size_tl)

autos=

"hello world"s;

//字串字面量加s字尾會通過上面的運算子建立String如果只是拿到乙個 char* 或者 const char* 之類的來判斷的話應該是沒有辦法(或許Windows下可以通過獲取記憶體屬性來判斷?)

如何在編譯器判斷模版引數是否屬於某個容器型別,且容器的元素是否屬於算數型別?

Alinshans 是要這樣的效果?template typename typename std enable if is arithmetic key type value std is arithmetic value type value,S type constexpr bool equal...

請問如何在c 中判斷乙個變數是否存在?

Betty 來拋磚引玉一下。首先定義乙個標頭檔案 FIND VARIABLE.hpp ifndef FIND VARIABLE HPP INCLUDED define FIND VARIABLE HPP INCLUDED if defined MSC VER MSC VER 1020 pragma ...

既然編譯器可以判斷乙個函式是否適合 inline,那還有必要自己加 inline 關鍵字嗎?

飛鶴 沒必要加,是否inline完全編譯器決定的。只要函式短小,編譯器一般會自動內聯的。如果函式複雜,你即便加了inline編譯器也可能取消內聯的。 叛逆者 一般來說沒必要。作為成員函式來說,一行的短函式,你就實現在類定義裡面,不用寫inline。實現到cpp,也不用寫inline。沒有什麼理由讓乙...