小编tom*_*789的帖子

使用enable_if选择类构造函数

考虑以下代码:

#include <iostream>
#include <type_traits>

template <typename T>
struct A {
    int val = 0;

    template <class = typename std::enable_if<T::value>::type>
    A(int n) : val(n) {};
    A(...) { }

    /* ... */
};

struct YES { constexpr static bool value = true; };
struct NO { constexpr static bool value = false; };

int main() {
    A<YES> y(10);
    A<NO> n;
    std::cout << "YES: " << y.val << std::endl
              << "NO:  " << n.val << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我想有选择地使用enable_if为某些类型定义构造函数A :: A(int).对于所有其他类型,都有默认构造函数A :: A(...),当替换失败时,它应该是编译器的默认情况.然而这对我来说有意义编译器(gcc版本4.9.0 …

c++ templates constructor sfinae

47
推荐指数
3
解决办法
2万
查看次数

推导纯虚函数的实现

考虑以下示例

#include <iostream>

struct PureVirtual {
    virtual void Function() = 0;
};

struct FunctionImpl {
    virtual void Function() {
        std::cout << "FunctionImpl::Function()" << std::endl;
    }   
};

struct NonPureVirtual : public FunctionImpl, public PureVirtual {
    using FunctionImpl::Function;
};

int main() {
    NonPureVirtual c;
    c.Function();
}
Run Code Online (Sandbox Code Playgroud)

编译器(GCC 4.9,Clang 3.5)退出时出错

test.cpp:18:20: error: variable type 'NonPureVirtual' is an abstract class
    NonPureVirtual c;
                   ^
test.cpp:4:18: note: unimplemented pure virtual method 'Function' in 'NonPureVirtual'
    virtual void Function() = 0;
                 ^
Run Code Online (Sandbox Code Playgroud)

但是当我没有得到形式时,PureVirtual一切都还可以.这很奇怪,因为标准 10.4.4

如果一个类包含或继承至少一个最终覆盖为纯虚拟的纯虚函数,则该类是抽象的. …

c++ polymorphism abstract-class virtual-functions pure-virtual

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

从ARKit获取RGB"CVPixelBuffer"

我正试图CVPixelBuffer从Apple的ARKit 获得RGB色彩空间.在我获取实例的func session(_ session: ARSession, didUpdate frame: ARFrame)方法ARSessionDelegateARFrame.在页面上显示使用金属的AR体验我发现此像素缓冲区位于YCbCr(YUV)颜色空间中.

我需要将其转换为RGB色彩空间(我实际需要CVPixelBuffer而不是UIImage).我在iOS上找到了一些关于颜色转换的东西,但我无法在Swift 3中使用它.

ios swift metal cvpixelbuffer arkit

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

在Haskell中键入自动函数约束推导的约束

出于教育目的,我在Haskell玩树.我的Tree a类型定义如下

data Tree a = EmptyTree | Node a (Tree a) (Tree a)
Run Code Online (Sandbox Code Playgroud)

和很多共享基本约束的函数Ord a- 所以他们有类似的类型

treeInsert :: Ord a => a -> Tree a -> Tree a
treeMake :: Ord a => [a] -> Tree a
Run Code Online (Sandbox Code Playgroud)

等等.我也可以Tree a这样定义

data Ord a => Tree a = EmptyTree | Node a (Tree a) (Tree a)
Run Code Online (Sandbox Code Playgroud)

但我不能简化我的功能,省略额外Ord a的如下:

treeInsert :: a -> Tree a -> Tree a
treeMake :: [a] -> Tree a …
Run Code Online (Sandbox Code Playgroud)

haskell types type-deduction

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