小编Gre*_*kel的帖子

在CTRP派生类中没有名为"type"的类型

我一直在尝试使用奇怪的重复模板模式进行通用的单参数仿函数,并且有两个实现:一个使用模板模板参数工作,另一个我尝试访问接口类中的派生Functor ::类型.在后一个例子中,编译器(gcc 5.4.0)报告

错误:'struct Cube <double>中没有名为' type ' 的类型

template<class T, template<class> class Functor>
class FunctorInterface_1 {
private:
  const Functor<T> &f_cref;
public:
  FunctorInterface_1() : f_cref(static_cast<const Functor<T>&>(*this)) {}
  T operator() ( T val ) const { return f_cref(val); }
}; // FunctorInterface_1 (works)

template<class Functor>
class FunctorInterface_2 {
private:
  const Functor &f_cref;
public:
  using Ftype = typename Functor::type;
  FunctorInterface_2() : f_cref(static_cast<const Functor&>(*this)) {}
  Ftype operator() ( Ftype val ) const { return f_cref(val); }
}; // FunctorInterface_2 …
Run Code Online (Sandbox Code Playgroud)

c++ templates non-virtual-interface

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

C++ 检测类型是否有模板参数

我想统一一个接口来处理模板化和非模板化类型。有没有办法确定类型(例如类或函数指针)是否依赖于模板参数?

例如:

struct foo {};
template<typename T>  struct bar {};

// This works if the template parameter is provided
template<typename> struct is_templated : false_type {};
template<template<typename...> class Obj, typename...Args>
struct is_templated<Obj<Args...>> : true_type {};
template<typename T> constexpr auto is_templated_v = is_templated<T>::value;
Run Code Online (Sandbox Code Playgroud)

在这种情况下,is_template_v<foo>是假的,is_template_v<bar<int>>是真的,但我不能仅仅用 推断出任何东西is_template_v<bar>。或者,如果我定义

template<template<typename...> class> 
struct temp_check : true_type {};
Run Code Online (Sandbox Code Playgroud)

然后temp_check<bar>是完全有效的,但我不知道如何类似地检查foo. 如果它是有效的 C++,那么需要的是这样的东西

template<template<> class A> struct temp_check<A> : false_type {};
Run Code Online (Sandbox Code Playgroud)

是否有某种机制可以检查两者?

c++ templates sfinae

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

通过Cython(回调)将Python函数应用于std :: vector

我试图通过Cython学习如何在C和Python之间使用回调,并一直在看这个演示.我想将一个Python函数应用于一个std :: vector/numpy.array并将结果存储在另一个中.我可以编译并运行没有错误,但最终矢量y没有被更改.

C++标题

// callback.hpp
#include<vector> 
typedef double (*Callback)( void *apply, double &x );
void function( Callback callback, void *apply, vector<double> &x, 
               vector<double> &y );
Run Code Online (Sandbox Code Playgroud)

C++源码

// callback.cpp
#include "callback.hpp"
#include <iostream>
using namespace std;

void function( Callback callback,  void* apply,
               vector<double> &x,  vector<double> &y ) {

int n = x.size();

for(int i=0;i<n;++i) {
    y[i] = callback(apply,x[i]);
    std::cout << y[i] << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

Cython标题

# cy_callback.pxd
import cython
from libcpp.vector cimport vector

cdef extern from "callback.hpp":
     ctypedef …
Run Code Online (Sandbox Code Playgroud)

c++ python callback cython stdvector

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