小编use*_*087的帖子

如何将对int8_t的引用转换为对uint8_t的引用?

我尝试将int8_t的引用转换为uint8_t的引用.

我有以下代码:

inline mtype& operator&(mtype& mt, uint8_t& va) {
  // do something
  // ...

  return mt;
}

inline mtype& operator&(mtype& mt, int8_t& va) {
  // do the same but signed
  // ...

  return mt;
}
Run Code Online (Sandbox Code Playgroud)

因为这两种重载做同样的,我要(或更好的DRM),所以我想打电话与第一运营商casted va.但是我该怎么做?这不行.

inline mtype& operator&(mtype& mt, int8_t& va) {
  return mt& static_cast<uint8_t>(va); //  error: no match for 'operator&' in 'mt & (uint8_t)va'
}
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

c++ casting reference

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

显式调用模板化重载运算符

我有一个带有重叠运算符的类.

class sout {
public:
  template<typename T>
  friend inline sout& operator&(sout&, T&);

  friend inline sout& operator&(sout&, std::string&);
};
Run Code Online (Sandbox Code Playgroud)

现在,如果我使用模板化运算符和sting.operator内部,我得到一个错误:

码:

sout& operator&(sout& s, std::string& str) {
  uint16_t ts = static_cast<uint16_t>(str.size());  // this is ok
  s & ts; // is also ok

  s & static_cast<uint16_t>(str.size());  // but this is wrong

  // ...
  return s;
}
Run Code Online (Sandbox Code Playgroud)

错误:

Error:C2679: binary '&' : no operator found which takes a right-hand operand of type 'uint16_t' (or there is no acceptable conversion)
could be 'sout &operator …
Run Code Online (Sandbox Code Playgroud)

c++ templates casting operator-overloading

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

是否定义了std :: chrono :: duration中的std :: ratio计算顺序?

如果我达到使用的整数的限制(当前在我的实现中long long),我要么丢失第一个或最后一个数字,这取决于首先完成的内容(乘法或除法).查看我的实现乘法首先完成,所以我丢失了第一个数字.我的问题是,它是在某处(草案中最好的)首先定义的是什么?

编辑:

实现就像(缩短版):

count * num / den;
Run Code Online (Sandbox Code Playgroud)

boost的实现是一样的.

c++ c++11 c++-chrono

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

调用MSVC和gcc之间的约定

我有一个程序生成与MSVC动态加载DLL.dll提供从主程序调用的函数.如果两者都使用MSVC或gcc构建,一切都很好,但是当我编译例如main与MSVC和dll与gcc的东西是错误的.

#  ifdef __GNUC__
#    define CDECL __attribute__ ((__cdecl__))
#  else
#    define CDECL __cdecl
#  endif

struct EXP result {
    uint32_t code;
};

#define SUCCESS result{0};

virtual result CDECL foo(char const* const*& target) const {
    target = (char const* const*)0xAFFE;
    return SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

问题是,在调用目标为零而不是0xAFFE之后.主程序使用__cdecl调用约定编译.结构被打包(没有对齐),但我也尝试对齐不同的大小(1,2,4,8,16).我也尝试使用__declspec/__atribute__(dllexport)两种变体的不同组合.

如果我查看汇编代码,有两个很大的区别:

; MSVC                          |   gcc
;===============================|================================
; before calling                |
;-------------------------------|--------------------------------
                                |   sub     dword ptr [esp+4],8
                                |
; foo();                        |
;-------------------------------|--------------------------------
push    ebp                     |   push    ebp
mov …
Run Code Online (Sandbox Code Playgroud)

c++ gcc calling-convention visual-c++

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

如何使用和不使用正则表达式同时使用`grep`?

我有一个bash脚本,我在使用grep来查找文件中的文本(存储在变量中).

found=$(grep "^$line$" "$file")
Run Code Online (Sandbox Code Playgroud)

现在我的问题是,我需要grep才能使用正则表达式.但是,line有时包含带有正则表达式关键字的文本,例如[导致失败的文本grep: Unmatched [.

是否有可能让grep不解释$line正则表达式的内容?

bash grep

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

指向SDL音频回调的成员函数的指针?

我使用SDL 1.2.15打从libav(ffmpeg的)声音就像这个例子在这里

我已经宣布要播放声音的课程。但是现在我必须在中存储该回调函数的指针SDL_AudioSpec::callback(在示例中wanted_spec.callback)。但是我的回调是我班的成员。

SDL中的回调指针是:

void (SDLCALL *callback)(void *userdata, Uint8 *stream, int len);
Run Code Online (Sandbox Code Playgroud)

哪里SDLCALL__cdecl

如何在我的成员函数中存储一个指针wanted_spec_.callback

c++ pointers sdl callback member

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

在工作线程之间几乎平均分配数据集

我有n个元素和p个线程。我试图在线程之间尽可能平均地划分元素。

例如:

If n = 8 and p = 1, then [8]
If n = 8 and p = 2, then [4, 4]
If n = 8 and p = 3, then [2, 3, 3]
If n = 8 and p = 4, then [2, 2, 2, 2]
If n = 8 and p = 5, then [1, 1, 2, 2, 2]
If n = 8 and p = 6, then [1, 1, 1, 1, 2, …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm logic

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