Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在一种情况下,专门化模板类,使其行为类似于浮点_C++_Templates_Alias_Template Specialization - Fatal编程技术网

C++ 在一种情况下,专门化模板类,使其行为类似于浮点

C++ 在一种情况下,专门化模板类,使其行为类似于浮点,c++,templates,alias,template-specialization,C++,Templates,Alias,Template Specialization,想象一个向量类: template <size_t size> class Vector<size> { std::array<float, size> data; .... } 模板 类向量{ std::数组数据; .... } 如果大小为1,是否有方法将模板专门化为浮点? 比如: // The case of a Vector with size 1 should behave like a float template <&

想象一个向量类:

template <size_t size>
class Vector<size> {
    std::array<float, size> data;
    .... 
}
模板
类向量{
std::数组数据;
.... 
}
如果大小为1,是否有方法将模板专门化为浮点? 比如:

// The case of a Vector with size 1 should behave like a float
template <>
using class Vector<1> = float; 
//大小为1的向量的大小写应该类似于浮点
样板
使用类向量=浮点;
我想把它应用到其他课程中。 例如,将列大小为1的矩阵视为行大小为1的向量



提前感谢您的帮助:)

您可以使用专用模板进行类型选择(请注意,别名模板不允许进行部分专门化,因此无法仅使用一个模板):

模板结构
VectorType{using type=VectorImpl;};//VectorImpl是您当前的向量
模板结构
VectorType{using type=float;};
//别名模板
模板使用
Vector=类型名VectorType::type;

您可以使用专用模板进行类型选择(请注意,别名模板不允许进行部分专门化,因此无法仅使用一个模板):

模板结构
VectorType{using type=VectorImpl;};//VectorImpl是您当前的向量
模板结构
VectorType{using type=float;};
//别名模板
模板使用
Vector=类型名VectorType::type;

我现在不能检查。也许以下面的方式

template <>
class Vector<1> {
  float n;
 public:
  operator float& () { return n; }
}
模板
类向量{
浮动n;
公众:
运算符float&({return n;}
}

您可能需要执行其他必需的操作。

我现在无法检查它。也许以下面的方式

template <>
class Vector<1> {
  float n;
 public:
  operator float& () { return n; }
}
模板
类向量{
浮动n;
公众:
运算符float&({return n;}
}

您可能需要实现其他所需的运算符。

您的原始代码可能会违反,但我相信这可能是相当等效且相对较短的备选方案:

template <std::size_t N>
struct VectorImpl {
   std::array<float, N> data;
};

template <std::size_t N>
using Vector = typename std::conditional<(N == 1), float, VectorImpl<N>>::type;
模板
结构向量{
std::数组数据;
};
样板
使用Vector=typename std::conditional::type;

您的原始代码可能会违反,但我相信这可能是相当等效的、相对较短的替代方案:

template <std::size_t N>
struct VectorImpl {
   std::array<float, N> data;
};

template <std::size_t N>
using Vector = typename std::conditional<(N == 1), float, VectorImpl<N>>::type;
模板
结构向量{
std::数组数据;
};
样板
使用Vector=typename std::conditional::type;


你试过犰狳吗?我不知道你是否可以把
Vector
作为
float
的别名,但是你可以有一个
操作符float&
,它只适用于
Vector
@小威胁我正在编写自己的数学库,用于教育、学习和玩弄它,但是谢谢你的建议。添加一个
操作符float&
很好。用
浮点
替换整个东西是个坏主意。如果存在
Vector::size()
,则
Vector::size()
也应该存在。你试过犰狳吗?我不知道你能不能把
Vector
作为
float
的别名,但是你可以有一个
操作符float&
,它只适用于
Vector
@Minor Threat我正在写我自己的数学库,用于教育、学习和玩它,但是谢谢你的建议。添加一个
操作符float&
很好。用
浮点
替换整个东西是个坏主意。如果存在
Vector::size()
,那么
Vector::size()
也应该存在。对我来说似乎是一种可能性,谢谢。但是,它仍然会调用接受任何大小的向量的函数,而不是使用float调用重载版本,对吗?我不确定。我假设在这种情况下,可以报告歧义错误。对于任何大小的向量,应选择精确的浮点匹配,而不是模板函数。它确实会调用接受向量的函数(如果可用)。对于存在精确匹配的情况,它不会进行隐式转换。@patatahooligan谢谢,我希望用它减少一些性能损失,并简化一些事情,我想我必须采用VTT的解决方案。@SirHeadshot:这个想法不会导致性能损失。你一定是有别的原因造成的。对我来说似乎是可能的,谢谢你。但是,它仍然会调用接受任何大小的向量的函数,而不是使用float调用重载版本,对吗?我不确定。我假设在这种情况下,可以报告歧义错误。对于任何大小的向量,应选择精确的浮点匹配,而不是模板函数。它确实会调用接受向量的函数(如果可用)。对于存在精确匹配的情况,它不会进行隐式转换。@patatahooligan谢谢,我希望用它减少一些性能损失,并简化一些事情,我想我必须采用VTT的解决方案。@SirHeadshot:这个想法不会导致性能损失。你一定是有别的原因造成的。谢谢,看来效果不错。我想我只是希望有一个简短的解决方案,就像我原来问题中的一个想法。谢谢,这似乎很有效。我想我只是希望有一个简短的解决方案,就像我原来问题中的一个想法一样。