标签: partial-specialization

C++部分专业化(功能指针)

任何人都可以告诉我,以下是否合法c ++?

template < typename s , s & (*fn) ( s * ) > 
class c {};
Run Code Online (Sandbox Code Playgroud)

//部分专业化

template < typename s , s & (*fn) ( s * ) > 
class c < s*, s* & (*fn)(s**)  {};
Run Code Online (Sandbox Code Playgroud)

g ++(4.2.4)错误:函数调用不能出现在常量表达式错误中:模板参数2无效

虽然它确实适用于显式专业化

int & func ( int * ) { return 0; }
template <> class c < int , func> class c {};
Run Code Online (Sandbox Code Playgroud)

c++ partial-specialization specialization

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

类模板部分特化的问题

我一直在尝试实现一个需要部分模板特化并退回到静态结构技术的函数,但我遇到了许多问题。

            template<typename T> struct PushImpl<const T&> {
                typedef T* result_type;
                typedef const T& argument_type;
                template<int StackSize> static result_type Push(IStack<StackSize>* sptr, argument_type ref) {
                // Code if the template is T&
                }
            };
            template<typename T> struct PushImpl<const T*> {
                typedef T* result_type;
                typedef const T* argument_type;
                template<int StackSize> static result_type Push(IStack<StackSize>* sptr, argument_type ptr) {
                    return PushImpl<const T&>::Push(sptr, *ptr);
                }
            };
            template<typename T> struct PushImpl {
                typedef T* result_type;
                typedef const T& argument_type;
                template<int StackSize> static result_type Push(IStack<StackSize>* sptr, argument_type ref) …
Run Code Online (Sandbox Code Playgroud)

c++ templates partial-specialization specialization

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

部分特化不特化任何模板参数

我有以下代码,其中我试图制作模板化的安全数组迭代器。

template <typename T>
class SArrayIterator;

template <typename E>
class SArray;

class SArrayIteratorException;

template <typename T>
class SArrayIterator<T> {//<--line 16
        friend std::ostream &operator <<(std::ostream &os, const SArrayIterator<T> &iter);
public:
        SArrayIterator<T>(SArray<T> &sArr) : beyondLast(sArr.length()+1), current(0), sArr(sArr){}

        T &operator *(){
                if (current == beyondLast) throw SArrayIteratorException("Attempt to dereference 'beyondLast' iterator");
                return sArr[current];
        }   

        SArrayIterator<T> operator ++(){
                if (current == beyondLast) throw SArrayIteratorException("Attempt to increment 'beyondLast' iterator");
                current++;
                return *this;
        }   

        bool operator ==(const SArrayIterator<T> &other) {return sArr[current] == other.sArr[current];} 
        bool operator !=(const …
Run Code Online (Sandbox Code Playgroud)

c++ templates partial-specialization

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

具有从属名称的部分特化(typename)

我有以下简单的strinToTypeImpl函数将任何类型的字符串转换为模板类型.我担心的问题是编译器告诉我部分特化typename MyMatrix<T>::Vector3 :

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

我不能在专业化中使用依赖名称吗?

namespace details
{

    template<typename T>
    struct stringToTypeImpl{
        bool operator()(T& t, const std::string& s)
        {
          std::istringstream iss(s);
          return !(iss >> t).fail();
        }
    };


    template<typename T>
    struct stringToTypeImpl< typename MyMatrix<T>::Vector3  >{ 

        // Replacing typename MyMatrix<T>::Vector3  by 
        // Eigen::Matrix<T,3,1> WORKS but why?

        bool operator()(typename MyMatrix<PREC>::Vector3 & t, const std::string& s)
        {
          stringToVector3<PREC>(t,s);
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

c++ partial-specialization typename template-specialization

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

成对bool和c ++模板

我正在编写一个模板,它接受任意数量的参数,并在这些值上找到布尔AND.

template <bool... Vs> struct meta_bool_and;

template <bool V> struct meta_bool_and : std::integral_constant<bool, V> {}; 

template <bool V, bool... Vs> 
struct meta_bool_and : std::integral_constant<bool, V && meta_bool_and<Vs...>::value> {}; 
Run Code Online (Sandbox Code Playgroud)

但是,我无法通过以下消息进行编译

 error: redeclared with 2 template parameters
 struct meta_bool_and : std::integral_constant<bool, V && meta_bool_and<Vs...>::value> {}; 
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

c++ templates partial-specialization variadic-templates c++11

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

C++ - 重载模板化类方法,并对该方法进行部分说明

在堆栈溢出上已经有一些类似于此的问题,但似乎没有什么能直接回答我的问题.如果我重新发布,我会道歉.

我想重载一些模板化类的方法(带有2个模板参数),并对这些方法进行部分模板特化.我无法弄清楚正确的语法,并开始认为这是不可能的.我以为我会在这里发帖,看看能不能得到确认.

要遵循的示例代码:

template <typename T, typename U>
class Test
{
public:
    void Set( T t, U u ); 

    T m_T;
    U m_U;
};

// Fully templated method that should be used most of the time
template <typename T, typename U>
inline void Test<T,U>::Set( T t, U u )
{
    m_T=t;
    m_U=u;
}

// Partial specialisation that should only be used when U is a float.
// This generates compile errors
template <typename T>
inline void Test<T,float>::Set( T t, float …
Run Code Online (Sandbox Code Playgroud)

c++ templates overloading partial-specialization

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

功能模板部分特化的解决方法?

考虑以下关于积分pow的元函数(这只是一个例子):

class Meta
{
    template<int N, typename T> static constexpr T ipow(T x)
    {
        return (N > 0) ? (x*ipow<N-1>(x)) 
                       : ((N < 0) ? (static_cast<T>(1)/ipow<N>(x)) 
                                  : (1))
    }
};
Run Code Online (Sandbox Code Playgroud)

如何为这样的函数写停止条件?

c++ templates metaprogramming partial-specialization c++11

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

是否可以混合使用 SFINAE 和模板专业化?

这是我大致想要实现的目标:

// 声明
模板<类型名称...参数>
结构ArgsEstimate;

// 字符串的专门化,SFINAE 就有点过分了
模板<类型名称...参数>
struct ArgsEstimate<std::string&, Args...> {
    静态常量 std::size_t 大小 = 64 + ArgsEstimate<Args...>::size;
};

// 算术类型的特化
模板<类型名称 AirthmeticT,
         类型名 std::enable_if<std::is_arithmetic<AirthmeticT>::value>::type* = nullptr,
         类型名称...参数>
struct ArgsEstimate<AirthmeticT, Args...> {
    static const std::size_t size = sizeof(AirthmeticT) + ArgsEstimate<Args...>::size;
};

// 指针类型的特化
模板<类型名 PtrT,
         类型名 std::enable_if<std::is_pointer<PtrT>::value>::type* = nullptr,
         类型名称...参数>
struct ArgsEstimate<PtrT, Args...> {
    静态常量 std::size_t 大小 = 32 + ArgsEstimate<Args...>::size;
};

问题是,这段代码在我所做的点上给出了编译错误“模板参数在部分专业化中不可推导”enable_if。结构内部的Astatic_assert也不起作用,因为将会重新定义。

我知道,我可以单独使用 SFINAE 和函数重载来做到这一点。然而,对于像这样的情况std::string,使用 SFINAE 就有点矫枉过正了。

所以我想知道是否有混合模板专业化和 SFINAE 的干净方法。

c++ partial-specialization sfinae template-specialization

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