C++ 在编译时查询字符是否为数字

C++ 在编译时查询字符是否为数字,c++,character,compile-time,C++,Character,Compile Time,我想在编译时检查给定的字符是否是数字。我尤其需要实现以下功能原型: template<char c> constexpr bool IsDigit(); template<char C> constexpr bool IsDigit() { return C >= '0' && C <= '9'; } // ASCII only 模板 constexpr bool IsDigit(); 澄清一下:我不必使用自定义实现。如果在std中已经

我想在编译时检查给定的字符是否是数字。我尤其需要实现以下功能原型:

template<char c>
constexpr bool IsDigit();
template<char C>
constexpr bool IsDigit() { return C >= '0' && C <= '9'; }  // ASCII only
模板
constexpr bool IsDigit();

澄清一下:我不必使用自定义实现。如果在
std
中已经有一种内置方法,我更喜欢这种方法。

这应该适用于ASCII:

constexpr bool IsDigit(char c) { return c >= '0' && c <= '9'; }  // ASCII only

这适用于ASCII:

constexpr bool IsDigit(char c) { return c >= '0' && c <= '9'; }  // ASCII only

原型只能用于ASCII和其他单字节字符集(不是UTF-8!)。还有,为什么constexpr和template两者都有?(Unicode定义了ASCII以外的不同数字…)检查它是否是“0123456789”中的一个०१२३४५६७८९০১২৩৪৫৬৭৮৯੦੧੨੩੪੫੬੭੮੯૦૧૨૩૪૫૬૭૮૯୦୧୨୩୪୫୬୭୮୯௦௧௨௩௪௫௬௭௮௯౦౧౨౩౪౫౬౭౮౯೦೧೨೩೪೫೬೭೮೯൦൧൨൩൪൫൬൭൮൯๐๑๒๓๔๕๖๗๘๙໐໑໒໓໔໕໖໗໘໙༠༡༢༣༤༥༦༧༨༩၀၁၂၃၄၅၆၇၈၉႐႑႒႓႔႕႖႗႘႙០១២៣៤៥៦៧៨៩᠐᠑᠒᠓᠔᠕᠖᠗᠘᠙᧐᧑᧒᧓᧔᧕᧖᧗᧘᧙᱐᱑᱒᱓᱔᱕᱖᱗᱘᱙꘠꘡꘢꘣꘤꘥꘦꘧꘨꘩0123456789" :)或者使用std::isdigit()您的原型只能用于ASCII和其他单字节字符集(不是UTF-8!)。此外,为什么constexpr和template都可以?(除了ASCII之外,Unicode定义了不同的数字…)检查它是否是“0123456789”中的一个०१२३४५६७८९০১২৩৪৫৬৭৮৯੦੧੨੩੪੫੬੭੮੯૦૧૨૩૪૫૬૭૮૯୦୧୨୩୪୫୬୭୮୯௦௧௨௩௪௫௬௭௮௯౦౧౨౩౪౫౬౭౮౯೦೧೨೩೪೫೬೭೮೯൦൧൨൩൪൫൬൭൮൯๐๑๒๓๔๕๖๗๘๙໐໑໒໓໔໕໖໗໘໙༠༡༢༣༤༥༦༧༨༩၀၁၂၃၄၅၆၇၈၉႐႑႒႓႔႕႖႗႘႙០១២៣៤៥៦៧៨៩᠐᠑᠒᠓᠔᠕᠖᠗᠘᠙᧐᧑᧒᧓᧔᧕᧖᧗᧘᧙᱐᱑᱒᱓᱔᱕᱖᱗᱘᱙꘠꘡꘢꘣꘤꘥꘦꘧꘨꘩0123456789“:)或使用std::isdigit()