我一直在尝试实现一个需要部分模板特化并退回到静态结构技术的函数,但我遇到了许多问题。
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) 这是我最近试图解决的问题的简化版本.我有以下两个类:
class Container { }
class Container<T> : Container
{
T Value
{
get;
private set;
}
public Container(T value)
{
Value = value;
}
public T GetValue()
{
return Value;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想做:
Container<int> c1 = new Container<int>(10);
Container<double> c2 = new Container<double>(5.5);
List<Container> list = new List<Container>();
list.Add(c1);
list.Add(c2);
foreach (Container item in list)
{
Console.WriteLine(item.Value);
Console.WriteLine(item.GetValue());
}
Run Code Online (Sandbox Code Playgroud)
实现此功能的最佳方法是什么?有可能吗?我想我可能已经解决了这个问题,但我认为这是一个解决方案,我正在寻找一些设计模式.
Michal,请提前感谢您的回复.
PS
我试过接口,虚函数,抽象类,抽象函数; 甚至在超类中创建函数,可以通过名称调用实际类型的属性(使用反射)...我仍然无法实现我想要的...
我必须为模板类实现非成员函数isHomogenous(Triple triple),定义为:
template <typename T1, typename T2, typename T3>
class Triple
{
public:
Triple()
{ }
Triple(const T1 &a, const T2 &b, const T3 &c) : a(a), b(b), c(c)
{ }
...
Run Code Online (Sandbox Code Playgroud)
isHomogenous函数应返回一个bool值,指示参数triple中的所有三个值是否属于同一类型.我试过了:
template <typename T> bool isHomogenous(Triple<T, T, T> triple) {
return true;
}
template <typename T1, typename T2, typename T3> bool isHomogenous(Triple<T1, T2, T3> triple) {
return false;
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,你可以提示我解决方案吗?
如果我删除模板特化部分(试图打印"测试2"的部分),代码编译很好,但我希望能够有一个特殊情况,运行一个看起来干净的外部用户不同的代码路径.
#include <iostream>
using namespace std;
struct SpecialType {};
template<typename A , typename B = SpecialType>
class Test
{
public:
class TestInner
{
public:
TestInner& operator* ();
};
};
template<typename A , typename B>
typename Test<A , B>::TestInner& Test<A , B>::TestInner::operator* ()
{
cout << "Test 1" << endl;
return *this;
}
// If the following is removed, everything compiles/works, but I want this alternate code path:
template<typename A>
typename Test<A , SpecialType>::TestInner& Test<A , SpecialType>::TestInner::operator* ()
{
cout << …Run Code Online (Sandbox Code Playgroud) c++ templates partial-specialization specialization template-specialization
如果我有两个类,B是A和A的特化,有一个非虚函数叫做size:
class A
{
unsigned Size() {return sizeof(*this);}
}
class B: public A;
Run Code Online (Sandbox Code Playgroud)
当我从B调用Size函数时,它会返回B的大小还是A的大小?我假设B,因为this关键字会为每个关键字评估不同的东西,但我不确定.
我的模板有语法错误
我想部分专门化我的模板类的静态函数
template <typename Foo, size_t bar = 26>
class MyClass
{
MyClass();
static void function();
};
#include "class.tpp"
Run Code Online (Sandbox Code Playgroud)
template <typename Foo, bar>
MyClass<Foo, bar>::MyClass()
{ }
template <typename Foo>
inline
void
MyClass<Foo, 6>::function()
{
// ...
}
template <typename Foo>
inline
void
MyClass<Foo, 26>::function()
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
error: template definition of non-template
我只想实现MyClass<Foo, bar>::functionbar == 26和bar == 6
怎么做得好?谢谢
当库用户对模板类的模板参数使用错误类型时,如何实现错误消息?
\n\ntest.cpp(从这里改编)
\n\n#include <type_traits>\ntemplate <typename T, typename Enable = void>\nclass foo; // Sorry, foo<T> for non-integral type T has not been implemented.\n\ntemplate <typename T>\nclass foo<T, typename std::enable_if<std::is_integral<T>::value>::type>\n{ };\n\nint main()\n{\n foo<float> x;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n该代码未按预期编译。但我不能让编译器仅在用户使用错误的类型时才显示错误。
\n\n错误信息为g++ test.cpp
test.cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\ntest.cpp:11:13: error: aggregate \xe2\x80\x98foo<float> x\xe2\x80\x99 has incomplete type and cannot be defined\n foo<float> x;\nRun Code Online (Sandbox Code Playgroud)\n\n问题:它没有打印我想要的错误消息 ( Sorry, foo<T> for non-integral type T has not been implemented.)
我是 C++ 模板的初学者。我正在尝试使用模板计算阶乘并附上下面的代码。我想使用模板专业化替换 if(t==0) 部分,但直到现在我还没有这样做。请帮助
#include
template <class T>
class Factorial
{
public:
T factorial(T t)
{
if(t==0)
return 1;
fact[t] = t*factorial(t-1);
std::cout<<"t and fact[t] "<<t<<", "<<fact[t]<<std::endl;
return fact[t];
}
void Print(T t)
{
std::cout<<"fact["<<t<<"] = "<<fact[t]<<std::endl;
}
private:
T fact[100];
};
/*
std::constexpr bool isZero(int x)
{
if(x==0)
return true;
}
*/
template<>
class Factorial<0>
{
public:
int factorial(int x)
{
return 1;
}
void Print(int t)
{
std::cout<<"special fact["<<t<<"] = "<<1<<std::endl;
}
};
int main()
{
Factorial<int> …Run Code Online (Sandbox Code Playgroud) 问题来自计算机图形C ++项目,我想在其中计算比例场和3D矢量场的梯度。我们知道它们的梯度不同:比例场具有3D矢量梯度,而3D向量场具有3x3矩阵梯度。由于所有其他代码都相同,因此我正在使用模板来重用代码。但是我在专门化成员函数时遇到了一个问题,该成员函数具有用于计算不同数据类型的梯度的不同代码。最小化的代码如下:
//======== Main.cpp ========
#include "Render.h"
int main() {}
//======== Render.cpp ========
#include "Render.h"
//======== Render.h ========
#ifndef __RENDER_H__
#define __RENDER_H__
#include "VolumeGrid.h"
#endif
//======== VolumeGrid.h ========
#ifndef __VOLUMEGRID_H__
#define __VOLUMEGRID_H__
#include "Volume.h"
template < typename U >
class _Grid {
public:
const typename GradType<U>::GType grad(const Vector& x) const;
U * values = nullptr;
};
template <>
const Vector _Grid<float>::grad(const Vector& x) const {
return Vector();
}
template <>
const Matrix _Grid<Vector>::grad(const Vector& x) const {
return Matrix();
} …Run Code Online (Sandbox Code Playgroud) 我想专门X针对浮点类型的类的方法。以下代码可以编译并完美运行:
x.hpp:
template <typename T>
class X {
public:
...
T bucket_width(const BucketID index) const;
T bucket_min(const BucketID index) const;
T bucket_max(const BucketID index) const
...
};
Run Code Online (Sandbox Code Playgroud)
x.cpp:
...
template <typename T>
T X<T>::bucket_width(const BucketID index) const {
return bucket_max(index) - bucket_min(index) + 1;
};
template <>
float X<float>::bucket_width(const BucketID index) const {
return bucket_max(index) - bucket_min(index);
};
template <>
double X<double>::bucket_width(const BucketID index) const {
return bucket_max(index) - bucket_min(index);
};
...
Run Code Online (Sandbox Code Playgroud)
现在,类似于此答案,我将cpp文件更改为:
template <typename T> …Run Code Online (Sandbox Code Playgroud) specialization ×10
c++ ×9
templates ×8
c# ×1
c++17 ×1
function ×1
generics ×1
inheritance ×1
inline ×1
polymorphism ×1
sfinae ×1
static ×1
superclass ×1
this ×1