标签: specialization

float和double文字的模板类中的函数特化

我正在尝试找到一个解决方案,在模板类方法中有不变的数字文字.我正在制作一些与float或double类型一起使用的数学模板类.问题是文字因数据类型而异(例如浮点数为"0.5f",双数字为"0.5").到目前为止,我提出了两个解决方案.第一个假设代码:

template <typename T>
class SomeClass
{
    public:
        T doSomething(T x);
};

template <>
float SomeClass<float>::doSomething(float x)
{
    float y = 0.5f;
    /*
     * Do computations...
    */
    return x;
}

template <>
double SomeClass<double>::doSomething(double x)
{
    double y = 0.5;
    /*
     * Do computations...
    */
    return x;
}
Run Code Online (Sandbox Code Playgroud)

上面的方法强制为它使用的每种类型重写整个方法.

另一种方法:

template <typename T>
class SomeClass
{
    public:
        T doSomething(T x);

    private:
        T getValue();
};

template <typename T>
T SomeClass<T>::doSomething(T x)
{
    T y = getValue();
    /*
     * Do computations...
    */ …
Run Code Online (Sandbox Code Playgroud)

c++ templates function literals specialization

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

C++模板 - 专业功能

我有以下代码:

//1
template<typename T>
void c(T in) {
    cout << "Template c(" << in << ")" << endl;
}
//2
template<>
void c<>(int* in) { 
        cout << "Template specialization b(" << in << ")" <<endl;
}
//3
template<typename T>
void c(T* in) {
        cout << "Template for pointers c(" << in << ")" <<endl;
}
//..
int i = 8;
c(&i);
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下为什么在下面的示例编译器中选择函数#3,但是当我改变函数#2和#3的顺序时,编译器会选择函数#2吗?

c++ templates specialization

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

无法将具有特殊特征的Scala方法专门用作返回类型

trait Eq[@specialized -X] {
  def eq(x: X, y: X): Boolean
}

trait Keyed[@specialized(Int) X] {
  def eqOnKey: Eq[X]
}
Run Code Online (Sandbox Code Playgroud)

该方法eqOnKey不专门用于生成的类Keyed$mcI$sp.

我如何专门化这个方法,即eqOnKey$mcI$sp在类中使返回类型的方法Keyed$mcI$sp成为Eq$mcI$sp

scala specialization

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

在模板中是否有一种方法只为每个计时实例化编写一个特化?(纳秒,毫秒,秒等)

我有一个需要下一个类型的工作模板:int,float,double,std::chrono::nanoseconds,std::chrono::millisecondsstd::chrono::seconds.

该模板有一个成员函数一起工作int,floatdouble但我需要编写纳秒,另一个为毫秒,另一个用于秒一个专业化.

为了得到一个有效的代码,我不得不为每个std::chrono"类型" 编写一个基本相同代码的专门化.所以我失去了使用模板的优势.

我已经读过计时器"类型"是持续时间实例化的typedef.

有没有办法只写一个纳秒,毫秒和秒的特化?

以下是我的代码的样子:

// file: myclass.hh

template<typename T> 
class Myclass {
public:
    // Variable (I want to initialize m_max to the maximum value possible for the 
    // T type)
    T m_max ;

    // Constructor
    Myclass ( ) ;
};

// file: myclass.cc

#include <limits>
#include <chrono>

// Constructor for int, float, double, etc...
template<typename T> 
Myclass<T>::Myclass …
Run Code Online (Sandbox Code Playgroud)

c++ templates specialization c++11 c++-chrono

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

具有模板化类型的模板专业化

我想使用以下函数专门化一个类模板:

template <typename T>
class Foo
{
public:
    static int bar();
};
Run Code Online (Sandbox Code Playgroud)

该函数没有参数,应根据Foo的类型返回结果.(在这个玩具示例中,我们返回该类型的字节数,但在实际应用程序中我们要返回一些元数据对象.)专门化适用于完全指定的类型:

// specialization 1: works
template <>
int Foo<int>::bar() { return 4; }

// specialization 2: works
template <>
int Foo<double>::bar() { return 8; }

// specialization 3: works
typedef pair<int, int> IntPair;
template <>
int Foo<IntPair>::bar() { return 2 * Foo<int>::bar(); }
Run Code Online (Sandbox Code Playgroud)

但是,我想将此概括为依赖于(其他)模板参数本身的类型.添加以下特化会产生编译时错误(VS2005):

// specialization 4: ERROR!
template <>
template <typename U, typename V>
int Foo<std::pair<U, V> >::bar() { return Foo<U>::bar() + Foo<V>::bar(); }
Run Code Online (Sandbox Code Playgroud)

我假设这不是合法的C++,但为什么呢?有没有办法优雅地实现这种类型的模式?

c++ templates specialization

9
推荐指数
2
解决办法
3134
查看次数

模板专业化

我有以下模板集:

//1  
template< typename T > void funcT( T arg )  
{
    std::cout<<"1: template< typename T > void funcT( T arg )";  
}  
//2  
template< typename T > void funcT( T * arg )  
{
    std::cout<<"2: template< typename T > void funcT( T * arg )";  
}  
//3  
template<> void funcT< int >( int arg )  
{  
    std::cout<<"3: template<> void funcT< int >( int arg )";  
}  
//4  
template<> void funcT< int * >( int * arg )  
{  
    std::cout<<"4: …
Run Code Online (Sandbox Code Playgroud)

c++ templates specialization

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

具有多个模板参数的C++单模板特化

喂!

我想只专注于两种模板类型中的一种.例如,template <typename A, typename B> class X对于单个函数应该有一个特殊的实现X<float, sometype>::someFunc().

示例代码:

main.h:

#include <iostream>

template <typename F, typename I>
class B
{
public:
    void someFunc()
    {
        std::cout << "normal" << std::endl;
    };

    void someFuncNotSpecial()
    {
        std::cout << "normal" << std::endl;
    };
};

template <typename I>
void B<float, I>::someFunc();
Run Code Online (Sandbox Code Playgroud)

main.cpp中:

#include <iostream>
#include "main.h"

using namespace std;

template <typename I>
void B<float, I>::someFunc()
{
    cout << "special" << endl;
}

int main(int argc, char *argv[])
{
    B<int, int> …
Run Code Online (Sandbox Code Playgroud)

c++ templates specialization

9
推荐指数
2
解决办法
2万
查看次数

如何在Scala中发现装箱/拆箱

以下建议通过即兴最近有关如何获取斯卡拉告诉我是否有拳击通过查看字节码的时候,我创建了这个类:

class X { def foo(ls : Array[Long]) = ls map (_.toDouble)
Run Code Online (Sandbox Code Playgroud)

看看字节码foo:

public double[] foo(long[]);
  Code:
   Stack=4, Locals=2, Args_size=2
   0:   getstatic       #11; //Field scala/Predef$.MODULE$:Lscala/Predef$;
   3:   aload_1
   4:   invokevirtual   #16; //Method scala/Predef$.longArrayOps:([J)Lscala/collection/mutable/ArrayOps;
   7:   new     #18; //class X$$anonfun$foo$1
   10:  dup
   11:  aload_0
   12:  invokespecial   #22; //Method X$$anonfun$foo$1."<init>":(LX;)V
   15:  getstatic       #27; //Field scala/Array$.MODULE$:Lscala/Array$;
   18:  getstatic       #32; //Field scala/reflect/Manifest$.MODULE$:Lscala/reflect/Manifest$;
   21:  invokevirtual   #36; //Method scala/reflect/Manifest$.Double:()Lscala/reflect/AnyValManifest;
   24:  invokevirtual   #40; //Method scala/Array$.canBuildFrom:(Lscala/reflect/ClassManifest;)Lscala/collection/generic/CanBuildFrom;
   27:  invokeinterface #46,  3; //InterfaceMethod scala/collection/TraversableLike.map:(Lscala/Function1;Lscala/collection/generic/CanBuildFrom;)Ljava/lan                                   g/Object;
   32:  checkcast       #48; …
Run Code Online (Sandbox Code Playgroud)

autoboxing scala specialization

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

继承专业化的模板参数推导

考虑以下代码:

#include <type_traits>

template < typename > struct BB { };
template < >          struct BB<float> : BB<int> { };
                      struct DD : BB<float> { };

template < typename... Args >
void ff(BB<Args...>) { }

int main()
{
    ff(BB<float>{});
    ff(DD{}); // FAILS! 'BB<Args ...>' is an ambiguous base class of 'DD'
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

调用调用ff(DD{})无法编译,因为gcc-8.3不想从BB<float>和中选择一个BB<int>clang执行相同操作)。但是BB<float> isa BB<int>,为什么BB<float>不可以选择?

问题是:这是否符合标准?在定义ffBB帮助gcc-8.3选择时是否有解决方法 …

c++ inheritance templates specialization language-lawyer

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

暧昧的模板怪异

我有以下代码(抱歉大代码块,但我不能再缩小它)

template <bool B>
struct enable_if_c {
      typedef void type;
};

template <>
struct enable_if_c<false> {};

template <class Cond>
struct enable_if : public enable_if_c<Cond::value> {};

template <typename X>
struct Base { enum { value = 1 }; };

template <typename X, typename Y=Base<X>, typename Z=void>
struct Foo;

template <typename X>
struct Foo<X, Base<X>, void> { enum { value = 0 }; };

template <typename X, typename Y>
struct Foo<X, Y, typename enable_if<Y>::type > { enum { value = 1 }; …
Run Code Online (Sandbox Code Playgroud)

c++ gcc templates specialization

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