在C ++中设置FourCC值 [英] Set a FourCC value in C++

查看:153
本文介绍了在C ++中设置FourCC值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C ++中设置 FourCC 值,即无符号的4字节整数。

I want to set a FourCC value in C++, i.e. an unsigned 4 byte integer.

我想,明显的方式是一个#define,例如。

I suppose the obvious way is a #define, e.g.

#define FOURCC(a,b,c,d) ( (uint32) (((d)<<24) | ((c)<<16) | ((b)<<8) | (a)) )

然后:

uint32 id( FOURCC('b','l','a','h') );

您认为最优雅的方式是什么?

What is the most elegant way you can think to do this?

推荐答案

您可以使用以下语句将其设为编译时常量:

You can make it a compile-time constant using:

template <int a, int b, int c, int d>
struct FourCC
{
    static const unsigned int value = (((((d << 8) | c) << 8) | b) << 8) | a;
};

unsigned int id(FourCC<'a', 'b', 'c', 'd'>::value);

有一点额外的努力,你可以在编译时检查每个传入的数字是在0和255.

With a little extra effort, you can make it check at compile time that each number passed in is between 0 and 255.

这篇关于在C ++中设置FourCC值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆