小编drw*_*owe的帖子

如何强制使用模板专业化?

我试图让我的模板函数产生编译时错误,如果实例化非专用基本版本.我尝试了通常的编译时断言模式(负数组大小),但即使没有实例化模板,编译也会失败.当且仅当基础模板函数被实例化时,有关如何使其失败的任何想法?

template<class Foo> void func(Foo x) {
  // I want the compiler to complain only if this function is instantiated.
  // Instead, the compiler is complaining here at the declaration.
  int Must_Use_Specialization[-1];
}

template<> void func(int x) {
  printf("Hi\n");
}
Run Code Online (Sandbox Code Playgroud)

c++ template-specialization

13
推荐指数
4
解决办法
3463
查看次数

Haskell:获取表达式的静态类型

我正在寻找一个能够执行GHCi:type命令的功能.

理想情况下,它会有类似的签名

getStaticType :: a -> String

a = getStaticType (1+2)
-- a = "(Num t) => t"

b = getStaticType zipWith
-- b = "(a -> b -> c) -> [a] -> [b] -> [c]"
Run Code Online (Sandbox Code Playgroud)

(注意:这与Data.Dynamic无关.我只想从编译器推断出静态类型.实际上该函数根本不需要运行时实现,因为对它的所有调用都可以在编译时作为常量内联时间.我假设它存在于某处,因为GHCi可以做到这一点)

haskell types ghc

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

签名饱和添加64位整数?

我正在寻找一些用于签名饱和64位加法的C代码,它使用gcc优化器编译为高效的X86-64代码.便携式代码是理想的,尽管如果需要可以使用asm解决方案.

static const int64 kint64max = 0x7fffffffffffffffll;
static const int64 kint64min = 0x8000000000000000ll;

int64 signed_saturated_add(int64 x, int64 y) {
  bool x_is_negative = (x & kint64min) != 0;
  bool y_is_negative = (y & kint64min) != 0;
  int64 sum = x+y;
  bool sum_is_negative = (sum & kint64min) != 0;
  if (x_is_negative != y_is_negative) return sum;  // can't overflow
  if (x_is_negative && !sum_is_negative) return kint64min;
  if (!x_is_negative && sum_is_negative) return kint64max;
  return sum;
}
Run Code Online (Sandbox Code Playgroud)

写入的函数产生具有多个分支的相当长的汇编输出.有关优化的提示吗?看起来它应该只用一个带有一些CMOV指令的ADD来实现,但我对这些东西有点生疏.

c optimization x86-64 addition saturation-arithmetic

11
推荐指数
4
解决办法
1919
查看次数

基于类成员的存在/缺席专门化C++模板?

考虑以下:

struct A {
  typedef int foo;
};

struct B {};

template<class T, bool has_foo = /* ??? */>
struct C {};
Run Code Online (Sandbox Code Playgroud)

我想专门ç,因此C <A>得到一个专业化和C <B>获取其他,基于存在或不存在类型名称的T :: foo中.这可能是使用类型特征或其他模板魔术吗?

问题是我尝试的所有内容在实例化C <B>时都会产生编译错误,因为B :: foo不存在.但这就是我想要测试的!


编辑:我认为ildjarn的答案更好,但我终于提出了以下C++ 11解决方案.男人是hacky,但至少它很短.:)

template<class T>
constexpr typename T::foo* has_foo(T*) {
  return (typename T::foo*) 1;
}
constexpr bool has_foo(...) {
  return false;
}
template<class T, bool has_foo = (bool) has_foo((T*)0)>
Run Code Online (Sandbox Code Playgroud)

c++ templates type-traits template-specialization

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

从std :: set :: insert()返回迭代器是const吗?

根据C++引用,set :: insert应该返回迭代器指向新插入元素的对,或者现有元素(如果存在).

但我有一个问题,分配给迭代器,因为这个简单的例子显示:

int main() {
  set<int> set;
  *set.insert(5).first = 5;
  return 0;
} 
Run Code Online (Sandbox Code Playgroud)

我尝试过G ++和Clang,但都不行.

set.cc:7:24: error: read-only variable is not assignable
  *set.insert(5).first = 5;
  ~~~~~~~~~~~~~~~~~~~~ ^
Run Code Online (Sandbox Code Playgroud)

我在文档中找不到任何指示迭代器应该遵循const对象的内容,类型签名中的任何内容都不会指示这一点.有人可以帮助我理解为什么这不起作用?

c++ stl

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

用GCC向量内在函数计算x ^ y

假设我有一个2元素向量定义如下(使用GCC语法打包向量)

// packed vector of 2-elements
typedef double v2d __attribute__((vector_size(sizeof(double)*2)));

v2d x = ...;
double y = ...;

x[0] = pow(x[0], y)
x[1] = pow(x[1], y)
Run Code Online (Sandbox Code Playgroud)

我想知道是否有更快的方法使用向量运算进行两次幂计算.该架构是x86-64上的GCC,平台特定代码是可以的.

c optimization performance gcc sse

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

const_cast类构造函数中的const成员

当我希望类的成员变量在类的生命期间保持不变时,我有时会使用const_cast,但它在构造函数中需要是可变的.例:

struct qqq {
 const vector<foo> my_foo;

  qqq(vector<foo>* other) {
    vector<foo>& mutable_foo = const_cast<vector<foo>&>(my_foo)
    other->swap(mutable_foo);
  }
};
Run Code Online (Sandbox Code Playgroud)

我曾经假设在构造函数中执行此操作基本上没问题,因为此时没有其他人依赖它,因此它不会与优化等交互不良.

然而,最近有人告诉我这是"未定义的行为",并且在任何情况下构造const对象之后改变const对象基本上是非法的.

有人可以澄清吗?这是一个不好/未定义的行为/事情吗?

c++ constructor const

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

rvalue refs和std :: move

有人可以解释为什么B不编译,但C呢?我不明白为什么std :: move是必需的,因为变量已经是rvalue ref.

struct A {
  int x;
  A(int x=0) : x(x) {}
  A(A&& a) : x(a.x) { a.x = 0; }
};

struct B : public A {
  B() {}
  B(B&& b) : A(b) {}  // compile error with g++-4.7
};

struct C : public A {
  C() {}
  C(C&& c) : A(std::move(c)) {}  // ok, but why?
};
Run Code Online (Sandbox Code Playgroud)

c++ rvalue-reference c++11

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

为什么这个Haskell类型类代码不起作用?

试图弄清楚Haskell类型类.以下为什么不工作?

{-# LANGUAGE FlexibleInstances #-}
class IntClass t
instance IntClass Int

intToIntClass  :: (IntClass r) => Int -> r
intToIntClass  x = x
Run Code Online (Sandbox Code Playgroud)

显然,"实例"并不意味着我认为它应该意味着什么.相反,它给出了错误消息,我不明白.

Could not deduce (r ~ Int)
from the context (IntClass r)
  bound by the type signature for intToIntClass  :: IntClass r => Int -> r
  at f.hs:10:1-16
  `r' is a rigid type variable bound by
      the type signature for intToIntClass  :: IntClass r => Int -> r
      at f.hs:10:1
In the expression: x
In an equation …
Run Code Online (Sandbox Code Playgroud)

haskell typeclass

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