#include
#include
#include
int main()
{
static const char* b2s[] = { "no", "yes" };
printf("%s\n", b2s[std::is_same::value]);
}
回报yes在Linux编译的时候,和no当OSX编译。
我在阅读的同时试着理解为什么这是apparently normal。 如果我正在开发一个库并且希望它是可移植的,我该如何处理这个问题?我需要处理每个操作系统的偏好吗?
下面是一个例子:
foo.h中
#include
template
struct Foo
{
static const char id;
};
Foo.cpp中
#include "foo.h"
#define DEFINE_FOO(type_,id_) \
template <> const char Foo::id = id_; \
template <> const char Foo::id = id_;
DEFINE_FOO( bool,'a')
DEFINE_FOO( int8_t,'b')
DEFINE_FOO(uint8_t,'c')
DEFINE_FOO(int16_t,'d')
DEFINE_FOO(uint16_t,'e')
DEFINE_FOO(int32_t,'f')
DEFINE_FOO(uint32_t,'g')
DEFINE_FOO(int64_t,'h')
DEFINE_FOO(uint64_t,'i')
DEFINE_FOO( float,'j')
DEFINE_FOO( double,'k')
// OSX requires this, but Linux considers it a re-definition
// DEFINE_FOO(unsigned long,'l')
由于我的图书馆的一部分,以前被编译并链接当我需要创建一个可执行文件时(比如main.cpp)。通常情况下,这看起来像:
$(CC) -c -Wall -std=c++0x -o foo.o foo.cpp
$(CC) -Wall -std=c++0x -o main main.cpp foo.o
这将工作在一个平台上,但在另一个失败;如果我在foo.cpp中取消注释DEFINE_FOO(unsigned long,'l'),那么OSX很高兴,但是Linux说Foo正在被重新定义。反之亦然。
这怎么可能是正常的?有没有一种“便携式”的方式来解决这个问题?
2016-04-23
Sheljohn