标签: specialization

const指针指向const类型的模板特化

我正在阅读http://bartoszmilewski.wordpress.com/2009/10/21/what-does-haskell-have-to-do-with-c/并遇到此代码以检查类型是否为指针:

template<class T> struct
isPtr {
 static const bool value = false;
};

template<class U> struct
isPtr<U*> {
 static const bool value = true;
};

template<class U> struct
isPtr<U * const> {
 static const bool value = true;
};
Run Code Online (Sandbox Code Playgroud)

我如何专门化通用模板来处理const指针到const类型的情况?如果我这样做:

std::cout << isPtr <int const * const>::value << '\n';
Run Code Online (Sandbox Code Playgroud)

当我期待虚假时,我得到一个真实的.谁能解释一下?

编辑:使用VC++ 2010编译器(快递:-)

c++ templates specialization

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

C++ - 未调用函数模板特化

我有以下代码:

template<typename T>
bool validate(const T& minimum, const T& maximum, const T& testValue)
{
    return testValue >= minimum && testValue <= maximum;
}

template<>
bool validate<const char&>(const char& minimum, const char& maximum, const char& testValue)
{
    // Allows comparisons with char arguments, ignoring case    
    // Localize by calling previously defined function
    return validate(toupper(minimum), toupper(maximum), toupper(testValue));
}
Run Code Online (Sandbox Code Playgroud)

第一个模板用于任何输入的类型,专门化用于文字字符.代码编译并运行main.cpp来测试它,但经过测试,我发现没有调用特化.它调用主模板.我无法弄清楚为什么.

c++ templates function character specialization

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

从不同的基类继承类模板特化是否合法?

我遇到过一种情况,我的类模板部分特化分享了很多代码,将它移到基类中是有意义的.但是,对于具有相同基类的所有特化,没有意义.

以下示例代码在GCC 7.1中编译而没有错误:

struct foo_base_1 { void bar() { std::cout << "base 1" << std::endl; }; };

struct foo_base_2 { void bar() { std::cout << "base 2" << std::endl; }; };

template <typename A, typename B>
struct foo { };

template <typename A>
struct foo<A, int> : foo_base_1 { };

template <typename A>
struct foo<A, double> : foo_base_2 { };

int main()
{
    foo<int, int> x;
    foo<int, double> y;

    x.bar();
    y.bar();
}
Run Code Online (Sandbox Code Playgroud)

我意识到,尽管是同一类的专业,但它们实际上是不同的类型.不过,这感觉不对,同样的类可以从不同的基础继承.

我想要的是一些保证,这是可以的.我找不到标准的相关部分,我不愿意相信它只是因为它编译(我以前被咬过).

c++ inheritance templates specialization

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

函数模板特化的显式实例化

我正在尝试创建一个专门用于某些给定类型的全局函数模板。它看起来像这样:

(主模板,模板特化,extern)

template <typename T> void foo() { std::cout << "default stuff" << std::endl; }
template<> void foo<int>() { std::cout << "int stuff" << std::endl; }
extern template void foo<int>();
Run Code Online (Sandbox Code Playgroud)

A.cpp(显式实例化)

template void foo<int>();
Run Code Online (Sandbox Code Playgroud)

void bar();
Run Code Online (Sandbox Code Playgroud)

B.cpp(包括啊)

 void bar() { foo<int>(); }
Run Code Online (Sandbox Code Playgroud)

主程序

foo<int>();
bar();
Run Code Online (Sandbox Code Playgroud)

编译器让我崩溃:“'void foo()' 的多个定义。我认为 extern 应该处理这个问题。B 编译单元不应该实例化 foo,而是在链接时使用 A 实例化,没有? 我哪里错了?

请注意,如果我不专门化 foo,则代码编译得很好。函数特化和实例化之间是否存在某种冲突?

c++ templates explicit instantiation specialization

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

分别为数据成员和成员函数专门化模板

我想专门使用一个模板来对指向数据成员的指针做一件事,对指向成员函数的指针做另一件事。这曾经在 gcc 11 之前一直有效,成员函数更具体。它仍然适用于 clang 11,但似乎与 gcc 中断了。

这是一个最小的非工作示例:

#include <iostream>

template<auto F> struct which;

template<typename K, typename V, K V::*F>
struct which<F> {
  static constexpr char desc[] = "pointer to data member";
};

template<typename K, typename V, K (V::*F)()>
struct which<F> {
  static constexpr char desc[] = "pointer to member function";
};

struct S {
  int i;
  int f() { return 0; }
};

int
main()
{
  std::cout << "S::i: " << which<&S::i>::desc << std::endl;
  std::cout << "S::f: " …
Run Code Online (Sandbox Code Playgroud)

c++ templates specialization c++17

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

命令在类模板中的函数的专门化中是否重要

考虑类似......

template<typename T>
class Vector {
  ...
  bool operator==( const Vector<float> &rhs ) {
    // compare and return
  }

  bool operator==( const Vector<T> &rhs ) {
    // compare and return
  }
  ...
 };
Run Code Online (Sandbox Code Playgroud)

注意专业化如何高于非专业版.如果我将专业版本放在非专业版本之下,那么Vector<float>==比较仍然可以按预期工作吗?出于某种原因,我想我还记得,如果你在这个场景中使用下面的专业化,那么当编译器查看标题时,它将首先看到默认值,看它是否有效,并使用它.

c++ templates specialization template-specialization

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

C++模板化函数可以选择成员变量吗?

我希望一个类具有一个带有模板参数的函数,并且基于该模板参数操作一个特定的成员变量.

例如,如果允许函数模板特化,那么像这样:

struct A
{
    struct M1 {};
    struct M2 {};

    // Function template specialization not allowed :(
    template<typename M>
    void addM(M const &m);

    template<>
    void addM(M1 const &m)
    {
        m1_vec_.push_back(m);
    }

    template<>
    void addM(M2 const &m)
    {
        m2_vec_.push_back(m);
    }

    std::vector<M1> m1_vec_;
    std::vector<M2> m2_vec_;
};
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?我觉得我错过了一些简单的东西,但不能完全把手指放在上面.

c++ templates function specialization

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

我的模板专业化调试版本与发行版本不同,这是gcc错误吗?

首先,我有一个类的头文件,一个没有定义的专业声明(来自互联网的代码示例)

$ cat foo.h

template<typename T>
class foo{
public:
  static void init(){
      return;
  }

};

template<>  void foo<int>::init();
Run Code Online (Sandbox Code Playgroud)

然后有2个用于模板专业化的实现文件

$ cat foo_int.cpp 
#include "foo.h"
#include<stdio.h>
template<>
void foo<int>::init(){
    printf("init int foo\n");
}

$ cat foo_float.cpp 
#include "foo.h"
#include<stdio.h>
template<>
void foo<float>::init(){
    printf("init float foo\n");
}
Run Code Online (Sandbox Code Playgroud)

终于我有了一个主文件

$ cat main.cpp
#include "foo.h"

int main(){
  foo<int>::init();
  foo<float>::init();
}
Run Code Online (Sandbox Code Playgroud)

如果我不进行优化就编译并运行它,它将得到:

g ++ foo_int.cpp foo_float.cpp main.cpp && a.out
init int foo
init浮点foo

如果添加优化,则结果将不同:

$ g ++ foo_int.cpp foo_float.cpp main.cpp -O2 && a.out
初始化int foo …

c++ templates symbols weak specialization

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

枚举值的模板专门化

是否可以专门为单个枚举值使用类方法?具体来说,我有一个枚举和一个类,如下所示:

#include <iostream>
#include <stdio.h>

using namespace std;

enum class Animal { dog, cat, bird  };
class Sound
{
   public:
      static void getSound ( const Animal& arg )
      {
         switch ( arg )
         {
           case Animal::dog:
             // dog specific processing
             break;

           case Animal::cat:
             // cat specific processing
             break;

           case Animal::bird:
             // bird specific processing
             break;

           default:
             return;
         }
      }
};
Run Code Online (Sandbox Code Playgroud)

我想专门针对每个枚举值的getSound函数,以摆脱开关的情况。这样的模板专业化可能吗?

c++ templates specialization sfinae

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

当两个模板参数属于同一类型时,如何执行部分专业化?

两个模板参数为同一类型如何部分专业化。

如何使用第二个函数编写此代码。

#include <utility>
#include <iostream>

template <typename A, typename B>
void Translate(A&& a,B* b){
  // make some translate from a to b
  // b->bvalue=a.av;
  std::cout<<"normal function";
}
//if a and b are same type,
template <typename A>
void Translate(A&& a, A* b) {
  *b = std::forward<A>(a);
  std::cout<<"forward function";
}

int main(int argc, char** argv) {
  int in=0,out=0;
  Translate(in,&out);
  return 0;
}

Run Code Online (Sandbox Code Playgroud)

期望输出“转发功能”。

c++ templates partial-specialization specialization overload-resolution

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