标签: specialization

特定成员函数的部分专业化

#include <iostream>

template <typename T1, typename T2>
class B{
public:
    void update(){ std::cerr<<__PRETTY_FUNCTION__<<std::endl; }
    void func1(){ std::cerr<<__PRETTY_FUNCTION__<<std::endl; }
    void func2(){ std::cerr<<__PRETTY_FUNCTION__<<std::endl; }
};

template <typename T1>
class B<T1, int>{
public:
    void update(){ std::cerr<<__PRETTY_FUNCTION__<<"(specialization)"<<std::endl;}
};

int main(){
    B<int, double> b1;
    b1.update();
    b1.func1();
    B<int, int> b2;
    b2.update();
    //b2.func1();//there's no function 'func1' in B<int,int>
}
Run Code Online (Sandbox Code Playgroud)

我想专门update针对特定模板参数(数据类型)的函数。

所以我尝试专业化template class B,但似乎我必须再次实现整个成员函数。

由于专业化之间的其他成员完全相同,因此重新实现整个成员看起来很麻烦。

对于这种情况有什么解决方法吗?

c++ templates partial-specialization specialization

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

是否可以检测指向成员函数的指针?

我想在指向成员函数的情况下使用特殊模板.有没有办法检测到这个?现在我声明struct isPtrToMemberFunc,然后为每个类添加一个额外的模板(类TType = void)(现在只是1)并专门化额外的模板以查看它是否为isPtrToMemberFunc.有没有办法自动检测到这个?如果不是我当前的方法是最好的解决方案?

c++ templates specialization

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

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
查看次数

模板<<和>>运算符专门化

我想在类中模拟>><<运算符,但我也想将它们专门用于字符串,所以我这样做了;

    class sql_command
{
public:
    sql_command() { }

    explicit sql_command(std::string& cmd)
        : m_raw(cmd) {
    }

    inline sql_command& operator<<(const char * arg)
    {
        m_raw += arg;
        return *this;
    }

    inline sql_command& operator<<(const std::string& arg)
    {
        m_raw += arg;
        return *this;
    }

    template<typename T>
    inline sql_command& operator>>(const T arg)
    {
        m_raw += boost::lexical_cast<std::string>(arg);
        return *this;
    }

    inline std::string const& command() const {
        return m_raw;
    }

private:
    std::string m_raw;
};

template<>
inline sql_command& operator>> <std::string> (const std::string& arg) { …
Run Code Online (Sandbox Code Playgroud)

c++ templates operators specialization

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

C++函数模板,Int参数问题

我很好奇为什么这不起作用:

const int ASSIGN_LEFT = 1;
const int ASSIGN_RIGHT = 2;

template <int AssignDirection>
void map( int& value1, int& value2 );

template<>
void map<ASSIGN_LEFT>( int& value1, int& value2 )
{  value1 = value2; }

template<>
void map<ASSIGN_RIGHT>( int& value1, int& value2 )
{  value2 = value1; }
Run Code Online (Sandbox Code Playgroud)

当我尝试使用此函数时,它会调用我先定义的模板特化.因此,map<ASSIGN_RIGHT>将调用map<ASSIGN_LEFT>上面的代码,除非我翻转专业化的顺序,然后它将始终调用map<ASSIGN_RIGHT>.

int main()
{
   int dog = 3;
   int cat = 4;

   map<ASSIGN_RIGHT>( dog, cat );
   std::cout << "dog= " << dog << ", cat= " << …
Run Code Online (Sandbox Code Playgroud)

c++ templates function specialization

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

如何在模板类中专门化模板成员函数?

例如:

template<unsigned number>
struct A
{
    template<class T>
    static void Fun()
    {}
};
Run Code Online (Sandbox Code Playgroud)

并想要专门化A <1> :: Fun()

template<>
A<1>::Fun<int>()
{
    /* some code here. */
}
Run Code Online (Sandbox Code Playgroud)

不起作用.怎么做?谢谢.

c++ templates specialization

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

Variadic递归模板mem有趣的专业化

这是我想写的代码:

template <typename T1, typename ... tail>
  class record : public record<tail...>
{
  using baseT = record<tail...>;

  T1 elem;

public:
  record(T1 first, tail... rest)
    : elem(first), baseT(rest...)
  {}

  template <int index>
  inline constexpr T1& get()
  {
        // line 83:
    return baseT::get<index-1>();
  }
  // line 85:
  template <>
  inline constexpr T1& get<0>()
  {
    return elem;
  }
};
Run Code Online (Sandbox Code Playgroud)

和我得到的编译器输出:( GCC 4.8.2 with -std=c++11)

record.hpp:85:15: error: explicit specialization in non-namespace scope 'class base::record<T1, tail>'
 template <>
       ^
record.hpp:86:33: error: template-id 'get<0>' in …
Run Code Online (Sandbox Code Playgroud)

c++ specialization variadic-templates c++11

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

或C++模板编程中的声明

我有一个大致像这样的程序.

#include <iostream>
#include <type_traits>

class Output
{
public:
    template <typename T>
    Output& operator&(T const& t)
    {
        std::cout << t << std::endl;

        return *this;
    }
};

class Input
{
public:
    template <typename T>
    Input& operator&(T& t)
    {
        std::cin >> t;

        return *this;
    }
};

class C
{
    public:
        int num1, num2;
};

template <typename T>
typename std::enable_if<std::is_same<T, Input>::value>::type operator&(T& t, C& c)
{
    t & c.num1 & c.num2;
}

template <typename T>
typename std::enable_if<std::is_same<T, Output>::value>::type operator&(T& t, C& c)
{ …
Run Code Online (Sandbox Code Playgroud)

c++ templates specialization c++11

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

如何专门化模板类成员函数?

请考虑以下示例:

    template <class T, class U>
class Test {
public:
    void f(){std::cout<<"f() not specized"<<std::endl;}
    void g(){std::cout<<"g() not specized"<<std::endl;}
    void h(){std::cout<<"h() not specized"<<std::endl;}

    //void g<long, double>(){}
};
Run Code Online (Sandbox Code Playgroud)

这里我对注释代码有错误.我想实现预期结果的唯一方法是将整个班级空间化.但是在下面提到的类中,我不能使用原始类的默认行为(例如f(f()和h()函数).

template <>
class Test<long, double> {
public:
    void f(){std::cout<<"f() specized long, double"<<std::endl;}
};
Run Code Online (Sandbox Code Playgroud)

那么有没有办法在原始类中对函数进行空间化?

c++ templates traits specialization template-specialization

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

const char*的模板方法专门化

请考虑以下代码段:

struct S {
    template <typename T>
    void insert(const T& x);
};

template <>
void S::insert<char*>(const char*& x) {}

int main() {
  S s;
  s.insert("");
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

gcc无法使用以下错误消息进行编译:

error: template-id 'insert<char*>' for 'void S::insert(const char*&)' does not match any template declaration
Run Code Online (Sandbox Code Playgroud)

这个错误的原因是什么,是否有办法编写专业化,以便它可以工作?

我不是在寻找替代解决方案,我只是想了解错误背后的逻辑.

c++ templates specialization template-specialization

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