标签: partial-specialization

C++模板部分特化错误

以下代码给出了一个编译错误: class Q64 is not a valid type for a template constant parameter

template<int GRIDD, class T>
INLINE T grid_residue(T amount) {
  T rem = amount%(GRIDD);
  if (rem > GRIDD/2) rem -= GRIDD;
  return rem;
}


template<int GRIDD, Q64>
INLINE Q64 grid_residue(Q64 amount) {
  return Q64(grid_residue<GRIDD, int64_t>(to_int(amount)));
}
Run Code Online (Sandbox Code Playgroud)

怎么了?我正在努力专攻grid_residueQ64.

更新:

改变了语法.现在得到错误error: function template partial specialization 'grid_residue<GRIDD, Q64>' is not allowed

template<int GRIDD>
INLINE Q64 grid_residue(Q64 amount) {
    return Q64(grid_residue<GRIDD, int>(to_int(amount)));
}
Run Code Online (Sandbox Code Playgroud)

谢谢

c++ templates g++ partial-specialization template-specialization

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

具有多个模板参数错误的模板部分特化

当我在具有一个模板参数的类上使用模板部分特化时,我可以专门化这样的方法:

#include <cstdlib>

template< std::size_t Dim >
class Test
{
public:
  int foo();
};

template< std::size_t Dim >
inline int Test< Dim >::foo()
{
  return 0;
}

template<>
inline int Test< 1 >::foo()
{
  return 1;
}

int main()
{
  Test< 2 > wTest2;
  Test< 1 > wTest1;
  wTest2.foo();
  wTest1.foo();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

方法foo专门用于Dim = 1.但是只要我向我的类添加模板参数,就像这样:

#include <cstdlib>

template< typename T, std::size_t Dim >
class Test
{
public:
  int foo();
};

template< typename T, std::size_t Dim >
inline int Test< …
Run Code Online (Sandbox Code Playgroud)

c++ templates compiler-errors metaprogramming partial-specialization

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

我应该怎么做而不是功能模板的部分特化?

我想写下面的内容:

template <typename S, typename T> void foo() {
    /* code for the general case */
}

template <typename T> void foo<MySType,T>() {
    /* partially specialized code - for any kind of T, but when S is MySType */
}
Run Code Online (Sandbox Code Playgroud)

或者,在其他情况下,以下内容:

template <typename S, typename T> void bar(const S& a, const T& b) {
    /* code for the general case */
}

template <typename T> void bar<MySType,T>(const MySType& a, const T& b) {
    /* partially specialized code - for any …
Run Code Online (Sandbox Code Playgroud)

c++ templates overloading partial-specialization template-specialization

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

模板类成员函数的C ++部分专业化

确实确实存在一些密切相关的问题,但是我正在努力找出如何应用其解决方案。

我有一个traits类,如下所示,用于处理我正在使用的boost::numeric:ublas::matrix矩阵(以及其他矩阵实现)。我只想对switch_storage_order注释中所示的进行部分专业化,但是由于函数无法部分进行专业化,因此这失败了。

我不想部分专门化该matrix_traits结构,因为这会带来重新定义其所有成员的开销。一种解决方案是将每个与矩阵相关的函数分成各自的结构,但最好将它们分组在一个特征类中。

有任何想法吗?也可以随意评论特质概念的一般应用。

#include <boost/numeric/ublas/matrix.hpp>

enum matrix_storage_order {row_major, column_major};

template<class matrix_type>
struct matrix_traits {
  // Default expects c-style row_major storage order.
  static matrix_storage_order get_storage_order(const matrix_type& m)
  { return row_major; }

  // By default can't change storage order so simply transpose.
  static void switch_storage_order(matrix_type& m) { m.transpose(); }
};

namespace ublas = boost::numeric::ublas;

/* This doesn't work with error C2244:
  * 'matrix_traits<matrix_type>::switch_storage_order' : unable to match function
  * definition to an existing declaration
  */ …
Run Code Online (Sandbox Code Playgroud)

c++ templates partial-specialization

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

C++ 特化模板类函数,无需重复代码

我想写 5 个不同的类,每个类都有许多完全相同的成员函数,除了一个,每个类都是特殊的。我可以写这个避免代码重复吗?

问候, 阿列克谢

下面是我的代码的一个非常缩短的版本,它引发了错误:

template_test.cpp:15:35: error: invalid use of incomplete type ‘class impl_prototype<cl, 1>
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
using namespace std;

template <int cl, int dim>
class impl_prototype {
public:
  impl_prototype() {}

  int f(int x) { return cl + 2 * g(x); }
  int g(int x) { return cl + 1 * x;}

};

template <int cl>
int impl_prototype<cl, 1>::g(int x) { return cl + 3 * x; }

int main ()
{
  impl_prototype<0, 0> test_0;
  impl_prototype<0, 1> test_1;


  cout …
Run Code Online (Sandbox Code Playgroud)

c++ templates class function partial-specialization

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

基于模板参数的可选范围检查

假设我有一个只对任何类型 T 执行加法的类。我想添加一个可选的范围检查(基于 bool 类型的模板参数),它将检查加法的结果是否属于给定的范围,或者否则它会抛出。这样做的一种方法是将类的所有基础知识包装在一个基类中,然后专门处理布尔模板参数。就像是:

// The base class; holds a starting value to add to and a maximum value
template<typename T>
class DummyImpl
{
private:
  T mval, mmax;

public:
  constexpr explicit DummyImpl(T x, T max_x) noexcept
 : mval{x}, mmax{max_x}
  {};

  // base class; use a virtual destructor
  virtual ~DummyImpl() {};

  T max() const noexcept {return mmax;}
  T val() const noexcept {return mval;}
};

// The "real" class; parameter B denotes if we want (or not)
// a range check …
Run Code Online (Sandbox Code Playgroud)

c++ templates partial-specialization nested-class c++11

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

部分专业化消歧优先链的更好模式?

考虑以下系列的部分专业化:

template <typename T, typename Enable=void>
struct foo {
  void operator()() const { cout << "unspecialized" << endl; }
};

template <typename T>
struct foo<T, enable_if_t<
  is_integral<T>::value
>>{
  void operator()() const { cout << "is_integral" << endl; }
};

template <typename T>
struct foo<T, enable_if_t<
  sizeof(T) == 4
    and not is_integral<T>::value
>>{
  void operator()() const { cout << "size 4" << endl; }
};

template <typename T>
struct foo<T, enable_if_t<
  is_fundamental<T>::value
    and not (sizeof(T) == 4)
    and not is_integral<T>::value
>>{ …
Run Code Online (Sandbox Code Playgroud)

c++ templates idioms partial-specialization

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

为什么SFINAE(enable_if)从类内部定义而不是外部工作

在过去的几个小时里,我一直在努力解决非常奇怪的问题(在我刚接触SFINAE并解决了5-6个其他问题之后)。基本上,在以下代码中,我希望可以f()处理所有可能的模板实例化,但是g()仅在N == 2以下情况下可用:

#include <type_traits>
#include <iostream>

template<typename T, int N>
class A
{
public:
    void f(void);
    void g(void);
};

template<typename T, int N>
inline void A<T, N>::f()
{
    std::cout << "f()\n";
}

template<typename T, int N, typename std::enable_if<N == 2, void>::type* = nullptr>
inline void A<T, N>::g()
{
    std::cout << "g()\n";
}

int main(int argc, char *argv[])
{
    A<float, 2> obj;
    obj.f();
    obj.g();

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

当我尝试对其进行编译时,我得到一个关于3个模板参数而不是2个模板参数的错误。然后,经过一番尝试,我决定将g()内部的定义移到其A自身的定义内,如下所示:

#include <type_traits> …
Run Code Online (Sandbox Code Playgroud)

c++ templates partial-specialization sfinae enable-if

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

部分特化中未使用的模板参数

我有以下代码:

template<typename T, typename Allocator = std::allocator<T> >
class Carray {
    // ...
    typedef T* pointer;
    typedef pointer iterator;
    // ...
};
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试进行部分专业化iterator_traits.对我来说似乎没问题,但g ++ 4.4.5抱怨:

#include <iterator>

namespace std {
    template<typename T, typename Allocator>
    struct iterator_traits<typename Carray<T, Allocator>::iterator> { // line 128
        typedef T value_type;
        typedef typename Allocator::difference_type difference_type;
        typedef typename Allocator::reference reference;
        typedef typename Allocator::pointer pointer;
        typedef typename std::random_access_iterator_tag iterator_category;
    };
}
Run Code Online (Sandbox Code Playgroud)

这是完整的错误消息:

carray.h:128: error: template parameters not used in partial specialization:
carray.h:128: error:         ‘T’
carray.h:130: error: …
Run Code Online (Sandbox Code Playgroud)

c++ partial-specialization iterator-traits

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

双模板函数实例化失败

如下代码:

template<typename T, MyEnum K> __global__ void myKernel(const T a[]);
template<typename T> __global__ void myKernel<T,SomeValueOfMyEnum>(const T a[]) {
    // implementation
}
Run Code Online (Sandbox Code Playgroud)

触发以下错误消息:

错误:此声明中不允许使用显式模板参数列表

为什么?

笔记:

  • 我很确定这与CUDA无关,只是一个C ++问题。
  • 关于局部专业化有很多问题,但是我不知道我是否是其中任何一个的骗子。

c++ templates cuda partial-specialization

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