小编ssk*_*itk的帖子

在typedef-ed结构上使用sizeof运算符

这可能是太明显的了.但是,我找不到具体的答案,尽管许多stackoverflow线程都在讨论这个问题的不同方面.

typedef struct _tmp {
   unsigned int a;
   unsigned int b;
} tmp;

int main()
{
    int c=10;
    if (c <= sizeof tmp) {
       printf("less\n");
    } else {
       printf("more\n");
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我编译这个编程为 -

g++ -lstdc++ a.cpp
Run Code Online (Sandbox Code Playgroud)

我收到一个错误 -

expected primary-expression before ‘)’ token
Run Code Online (Sandbox Code Playgroud)

我想我错过了一些非常明显和直截了当的事情.但似乎无法确定它: - /

谢谢!

c++ typedef

9
推荐指数
2
解决办法
6163
查看次数

QueryPerformanceCounter Linux等效

经过这个线程后,我了解到clock_gettime()和clock_getres()是在Linux环境中实现QueryPerformanceCounter和QueryPerformanceFrequency的选项.

许多示例建议读取clock_gettime返回的值并使用它.但那只是纳秒数而不是刻度数!因此,在Linux中获取滴答数的唯一方法是使用clock_gettime()和clock_getres()的组合.如果我错了,请纠正我.

所以我实现了我的QueryPerformanceCounter(它看起来非常难看!)所以想知道是否有更好的方法来实现Linux的QueryPerformanceCounter.我的代码如下 -

__int64 QueryPerformanceCounter()
{
    __int64 nsec_count, nsec_per_tick;
    /*
     * clock_gettime() returns the number of secs. We translate that to number of nanosecs.
     * clock_getres() returns number of seconds per tick. We translate that to number of nanosecs per tick.
     * Number of nanosecs divided by number of nanosecs per tick - will give the number of ticks.
     */
     struct timespec ts1, ts2;

     if (clock_gettime(CLOCK_MONOTONIC, &ts1) != 0) {
         printf("clock_gettime() failed");
         return -1;
     }

     nsec_count = …
Run Code Online (Sandbox Code Playgroud)

linux porting

6
推荐指数
0
解决办法
7357
查看次数

将__attribute__与typedef一起使用

如果问题看起来太明显或简单,请道歉.不幸的是,经过一堆线程和谷歌搜索关于typedef加上属性前缀,我仍然无法弄明白.

我在(据称)便携式应用程序中有以下代码片段 -

#ifdef WIN32
#define MY_PREFIX __declspec(dllexport)
#else
#define MY_PREFIX __attribute__((visibility("default")))
#endif

typedef MY_PREFIX bool some_func(void);
Run Code Online (Sandbox Code Playgroud)

所以我的问题是 -
1)那个typedef究竟在做什么?
2)代码在VS2008上编译正常,但在G ++(gcc-4.1)上,我收到警告"'visibility'属性被忽略"
有什么方法可以删除该警告吗?(省略-Wattributes不是一个选项)

谢谢!

c++ macros typedef g++ visual-c++

6
推荐指数
1
解决办法
2400
查看次数

在宏中为__FUNCTION__附加一个字符串

如果我问一个明显的问题,请原谅我,但经过一堆线程和尝试的东西,我无法确定这个简单的事情.

我有这个小程序:

#define FUNC_PREFIX __FUNCTION__ "() :"

int main()
{
    printf("%s\n", FUNC_PREFIX);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

所以我可以传递FUNC_PREFIX而不是__FUNCTION__记录函数,它们将打印调用函数名,后跟paren和冒号 - 这样可以提高日志行输出的可读性.

这在Visual Studio 2008中按原样编译.但是g++,在字符串常量之前,我得到一个错误')'

我做了一些像做的事情:

#define TEMP __FUNCTION__ 
#define FUNC_PREFIX TEMP "() :" 
Run Code Online (Sandbox Code Playgroud)

但无济于事.

这样做的方法是什么?

c++ macros

5
推荐指数
2
解决办法
4626
查看次数

如何找到几个字符之一的第一个出现(除了使用正则表达式)

通过一堆线程去后,我知道我需要使用regex.h使用在C&C++的正则表达式.

但我想知道,是否有更简单的方法来搜索字符串中"/"或"\"的出现.

// I have a strstr statement like this -
str = strstr(s, "/");
Run Code Online (Sandbox Code Playgroud)

我想知道是否可以更改它,以便我可以搜索第一次出现 / \在一次调用strstr.

c regex strstr

4
推荐指数
1
解决办法
7836
查看次数

使用NULL参数实例化模板类

我从其他Stackoverflow线程(如此)中了解到模块参数在编译时进行评估.此外,非类型模板参数应该是常量表达式,整型表达式或指向具有外部链接的对象的指针.

而且,我在Makefile中的g ++命令中没有使用--std = c ++ 0x.

那么,是否可以实例化一个模板类,其中NULL作为参数传递?

// I have a template class like this - 
template<class T, T invalidVal, int e> class A
{ 
    static inline bool dummy(T value) 
    {
       return 0;
    }
}

#define MY_INVALID_VAL ((void *)0)

// Now I want create class from the above template class with 
// T=void*, invalidVal=NULL & e=0 
typedef A<void *, MY_INVALID_VAL, 1> ClassA;
Run Code Online (Sandbox Code Playgroud)

上面的代码在MS Visual Studio 2008上编译得很好.在g ++上 - 我得到错误 - "转换为除了整数或枚举类型以外的类型不能出现在常量表达式中"

我在google搜索后尝试了一些选项 -

声明"extern void*MY_INVALID_VAL;" 在头文件中 - …

c++ templates g++

3
推荐指数
1
解决办法
6850
查看次数

#define _snprintf_s到snprintf

我正在将一个win32应用程序移植到linux上,而不是在每个_snprintf_s周围都有一堆#ifdef,我想知道是否有办法以某种方式#define它到snprintf.

所以像 -

#define _snprintf_s(1,2,3,4,5) snprintf(1,2,4,5)
Run Code Online (Sandbox Code Playgroud)

snprintf不存在第三个参数,即要存储的最大字符数或_TRUNCATE.

这种方法对吗?我能做这样的#define吗?如果是这样,有人可能会指出我应该怎么做呢?

我经历了这个问题,知道我必须小心这些#defines.

谢谢!

c visual-c++

2
推荐指数
2
解决办法
8385
查看次数

wchar_t for Linux和Windows之间的差异和转换

我理解这个这个线程在Windows中,wchar_t是16位&对于Linux,wchar_t是32位.

我有一个客户端 - 服务器架构(只使用管道 - 而不是套接字) - 我的服务器是基于Windows的,客户端是Linux.

Server有一个API来从客户端检索主机名.当客户端基于Windows时,它可以只执行GetComputerNameW并返回Wide-String.但是,当客户端基于Linux时,事情会变得混乱.

作为第一个天真的方法,我使用mbstowcs()希望将wchar_t*返回到Windows服务器端.但是,这个LPWSTR(我的linux clinet端有typedef wchar_t*LPWSTR)在Windows上无法识别,因为它希望它的wchar_t是16位.

那么,将linux上的gethostname()输出 - 在char*中转换为unsigned short(16位)是我唯一的选择吗?

提前致谢!

c++ g++ wchar visual-c++

0
推荐指数
1
解决办法
3715
查看次数

标签 统计

c++ ×5

g++ ×3

visual-c++ ×3

c ×2

macros ×2

typedef ×2

linux ×1

porting ×1

regex ×1

strstr ×1

templates ×1

wchar ×1