还有 char8_t呢?
我假设某个地方有一些C ++ 20决策,但我找不到。还有P1428,但DOC不提任何东西printf()家庭VS char8_t *或char8_t。
使用std::cout建议可能是一个答案。不幸的是,它不再编译了。
// does not compile under C++20
// error : overload resolution selected deleted operator '<<'
// see P1423, proposal 7
std::cout << u8"A2";
std::cout << char8_t ('A');
Run Code Online (Sandbox Code Playgroud)
对于C 2.x和char8_t
请从这里开始。
更新资料
我用u8序列中的单个元素做了更多测试。这确实是行不通的。char8_t *要printf("%s")工作,但char8_t到printf("%c")是等待发生的事故。
请参阅-https ://wandbox.org/permlink/6NQtkKeZ9JUFw4Sd-根据当前现状,问题char8_t尚未实现char8_t *。-让我重复一遍:没有实现的类型可以保存char8_t *序列中的单个元素。
如果您想要单个u8字形,则需要将其编码为u8字符串
char8_t const * single_glyph = u8"?";
Run Code Online (Sandbox Code Playgroud)
而且目前看来,打印上述一种肯定的方法是 …
注意:这是MSVC,C ++ 17问题。
免责声明:我知道已经尝试过了,是的,我正在尝试找到相关的SO答案。
我可以对UDL进行编码,以std::array在编译时将数字文字转换为:
// std::array{ '1','2','3' }
constexpr auto a_1 = 123_std_char_array;
// std::array{ '0','x','1','2' }
constexpr auto a_2 = 0x12_std_char_array;
// std::array{ '4'.'2','.','1','3' }
constexpr auto a_3 = 42.13_std_char_array;
Run Code Online (Sandbox Code Playgroud)
这是我创建的UDL:
template< char ... Chs >
inline constexpr decltype(auto) operator"" _std_char_array( )
{
// append '\0'
return std::array { Chs..., char(0) } ;
}
Run Code Online (Sandbox Code Playgroud)
令人惊叹,时髦,现代,等等,等等,等等。但是。
我如何编写一个UDL来实现此目的:
// std::array {'S','t','r','i','n','g'}
constexpr auto std_char_array_buff_ =
"String"_std_char_array ;
Run Code Online (Sandbox Code Playgroud)
请使用MSVC C ++ 17。
我知道要“捕获”字符串文字的UDL必须具有以下占用空间:
inline auto …Run Code Online (Sandbox Code Playgroud) ok, the latest VS 2019 Community, local "all defaults" C++ console project:
int main()
{
// cl Version 19.21.27702.2 for x86
//
constexpr auto MSCVER = _MSC_VER; // 1921
constexpr auto MSCFULLVER = _MSC_FULL_VER; //192127702
constexpr auto MSCBUILD = _MSC_BUILD; // 2
/*
: error C2131: expression did not evaluate to a constant
: message : failure was caused by non-constant arguments or reference to a non-constant symbol
: message : see usage of '__LINE__Var'
*/
constexpr auto LINE = …Run Code Online (Sandbox Code Playgroud) [这个问题有一个重复的问题,我可以找到,但这个答案是完全错误的,请参阅下面的 C 代码。]
据我了解,extern "C"C++ 中不会生成 C 代码。它只是一个链接指令。
我有一些这样的extern "C"故事要讲,但今天有一个让我困扰的故事。这是一个完全最新的VS2019,代码如下:
#include <stdint.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
// NOTE: in here it is still C++ code,
// extern "C" is a linkage directive
typedef struct Test Test;
struct Test {
/* remove this const and MSVC makes no warning
leave it in and MSVC complains, a lot
GCC or clang could not care less
*/
const
uint32_t x;
} ;
/* …Run Code Online (Sandbox Code Playgroud) ......为前一个道歉
在C++中我们如何执行以下操作
// fundamental language construct
type name = value ;
// for example
int x = y;
Run Code Online (Sandbox Code Playgroud)
用函数指针?
typedef (char)(*FP)(unsigned);
// AFAIK not possible in C++
FP x = y ;
Run Code Online (Sandbox Code Playgroud)
我可以用lambdas:
FP x = []( unsigned k) -> char { return char(k); }
Run Code Online (Sandbox Code Playgroud)
但我不知道如何在没有lambda的情况下做到这一点.有任何想法吗?