简体   繁体   English

如何使用 std::format c++20 打印 const GLubyte*?

[英]How to print const GLubyte* using std::format c++20?

I want to print the OpenGL version using the API const GLubyte *glGetString(GLenum name) using std::format().我想使用 API const GLubyte *glGetString(GLenum name) 使用 std::format() 打印 OpenGL 版本。
Trying the below code gives an error:尝试下面的代码会报错:

std::cout << std::format("OpenGL Version: {}\n", glGetString(GL_VERSION));

It can be easily printed using iostream with:它可以使用 iostream 轻松打印:

std::cout << "OpenGL Version: " << glGetString(GL_VERSION);

Tell the best way to print const GLubyte* using the c++20 feature std::format() which is faster than iostream?告诉使用比 iostream 更快的 c++20 功能 std::format() 来打印 const GLubyte* 的最佳方法? I am using Visual Studio 2019.我正在使用 Visual Studio 2019。

GLubyte is commonly implemented as a typedef of unsigned char , so the return type of glGetString(GL_VERSION) is const unsigned char* . GLubyte通常实现为unsigned char的类型定义,因此glGetString(GL_VERSION) typedef返回类型是const unsigned char*

std::format does not have a specialization of std::formatter for const unsigned char* , instead, you can use reinterpret_cast to convert it to const char* for output std::format没有对const unsigned char*std::formatter的专门化,相反,您可以使用reinterpret_cast将其转换为const char* for output

std::cout << std::format(
  "OpenGL Version: {}\n", 
  reinterpret_cast<const char*>(glGetString(GL_VERSION)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM