我有一个像
template <size_t N>
class A
{
template <size_t N>
someFunctions() {};
};
Run Code Online (Sandbox Code Playgroud)
现在我想创建类的实例并在 for 循环中为一组许多值调用其中的函数,例如
// in main()
int main()
{
for (int i = 1; i <= 100; i++)
{
const int N = i; // dont know how to do this
A<N> a;
a.functionCalls();
}
}
Run Code Online (Sandbox Code Playgroud)
这该怎么做?希望有一种方法可以做到这一点。
c++ templates for-loop compile-time-constant template-classes
我有一个真实的情况,可以在下面的例子中总结:
template< typename ListenerType >
struct Notifier
{
void add_listener( ListenerType& ){}
};
struct TimeListener{ };
struct SpaceListener{ };
struct A : public Notifier< TimeListener >
, public Notifier< SpaceListener >
{
};
struct B : TimeListener{ };
int main()
{
A a;
B b;
a.add_listener( b ); // why is ambiguous?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么编译器不明显B
是a TimeListener
,因此唯一可能的重载解决方案是Notifier< TimeListener >::add_listener( TimeListener& )
?
c++ multiple-inheritance overload-resolution name-lookup template-classes
假设我有一个这样的模板类:
template<typename TRequest, typename TResponse = void>
class handler
{
private:
TResponse process_core(const TRequest& request);
public:
TResponse process(const TRequest& request)
{
//log the request
TResponse response = process_core(request); //return process_core(request) works;
//log the response, for void it's fine to log nothing
return response;
}
};
Run Code Online (Sandbox Code Playgroud)
项目中的其他地方process_core
是针对不同的TRequest/TResponse类型实现的.例如:
template<> void handler<Foo>::process_core(const Foo& foo) { }
template<> Baz handler<Bar, Baz>::process_core(const Bar& bar) { }
Run Code Online (Sandbox Code Playgroud)
显然return response
打破了void
类型.这样做的正确方法是什么?或者我的设计不是C++方式?我是C++的新手.
可能重复:
GCC问题:使用依赖于模板参数的基类成员
我以为我熟悉C++,但显然不够熟悉.
问题是当您在模板类中定义常量时,可以在从该类派生的新类中使用常量,但不能从派生自它的新模板类中使用常量.
例如,gcc说
test.h:18:错误:未在此范围内声明'theconstant'
当我尝试编译这个(简化的)头文件时:
#pragma once
template <typename T> class base
{
public:
static const int theconstant = 42;
};
class derive1 : public base<size_t>
{
public:
derive1(int arg = theconstant) {}
};
template<typename T> class derive2 : public base<T>
{
public:
derive2(int arg = theconstant) {} // this is line 18
};
Run Code Online (Sandbox Code Playgroud)
所以问题是一个类derive1
编译得很好,但是另一个类derive2
,它是模板特化,却没有.
现在也许gcc的错误还不够清楚,但是我不明白为什么构造函数的derive2
范围不同于derive1
.
如果它很重要,这会在编译头文件本身时发生,而不是在实例化类型的对象时derive2<type>
.
我也知道要改进什么来进行编译,所以我并不是真的在寻找单行代码作为答案.我想知道为什么会这样!我尝试在网上搜索,但显然我没有使用正确的搜索参数.
我发现构造函数继承的语法略显奇怪.以下示例运行良好,但我不明白为什么我需要指定using sysTrajectory::sysTrajectory
而不是using sysTrajectory<Real>::sysTrajectory<Real>
从类模板继承?后者给出以下错误:expected ‘;’ before ‘<’ token using sysTrajectory<Real>::sysTrajectory<Real>;
.
class sysRealTrajectory: public sysTrajectory<Real>
{
public:
/**
* Default constructor
*/
inline sysRealTrajectory(void);
using sysTrajectory::sysTrajectory;
/**
* Default destructor
*/
inline ~sysRealTrajectory(void);
};
Run Code Online (Sandbox Code Playgroud)
主要:
Real a;
a=5;
sysTrajectoryPoint<Real> TP0(1.0,a);
sysRealTrajectory Trajectory(TP0);
Run Code Online (Sandbox Code Playgroud) 当我在我的main函数中调用模板类"add"和"greater"中的两个函数时,我一直得到未定义的引用.
所以,我有:number.h
#ifndef NUMBER_H
#define NUMBER_H
template <class T>
class number {
public:
T x;
T y;
number (int a, int b){
x=a; y=b;}
int add (T&);
T greater ();
};
#endif
Run Code Online (Sandbox Code Playgroud)
number.cpp
#include "number.h"
template <class T>
int number<T>::add (T& rezAdd){
rezAdd = x+y;
return 1;
}
template <class T>
T number<T>::greater (){
return x>y? x : y;
}
Run Code Online (Sandbox Code Playgroud)
我的主文件是:resolver.cpp
#include <stdio.h>
#include <stdlib.h>
#include "number.h"
int main (int argc, char **argv) {
int aux;
number<int> c(3,5);
c.add(aux);
printf …
Run Code Online (Sandbox Code Playgroud) 我正在尝试围绕以下类模板编写 API:
template <class A, class B>
class MyClass {
public:
A foo;
B bar;
MyClass();
MyClass(A in1, B in2) {
foo = in1; bar = in2;
}
void operator = (MyClass<A, B> in) {
foo = in.foo; bar = in.bar;
}
};
Run Code Online (Sandbox Code Playgroud)
作为此 API 的一部分,我希望实现者能够在不同类型的MyClass
. 为了实现这一点,我编写了以下函数:
template<class A, class B, class C, class D>
MyClass<C, D> operator MyClass<C, D> (MyClass<A, B> in) {
MyClass<C, D> out((C)in.foo, (D)in.bar);
return out;
}
Run Code Online (Sandbox Code Playgroud)
这段代码编译没有错误。
我的问题:
假设对于上述函数的任何给定实现都定义了C operator C (A) …
我目前正在设计一个接口(Base
在下面的例子中),它提供了几个虚拟方法,包括begin()
和end()
。这两个方法简单地返回相应的迭代器,就像在任何其他集合(如类)中一样。派生类应该实现这些方法并返回它们对迭代器的特定实现。
以下(简化的)示例显示了一个派生类,它使用 aboost::transform_iterator
来转换整数的私有内部列表。这个实现只是现实中的一个例子,迭代的“事物”可以是其他东西,迭代器也是如此。
该示例有效,但我有一个问题。对象类型main()
并没有隐藏使用的迭代器类型为 的事实TransformIterator
。基类将用于某种插件架构,其中每个插件都是一个共享库。插件不应该知道使用哪种类型的迭代器,而应该完全依赖于抽象接口。有没有办法做到这一点?
#include <boost/iterator/transform_iterator.hpp>
#include <iostream>
#include <string>
#include <vector>
template<class Iterator>
class Base
{
public:
virtual Iterator begin() = 0;
virtual Iterator end() = 0;
};
struct toString
{
std::string operator()(int number) const
{
return std::to_string(number);
}
};
typedef boost::transform_iterator<toString, std::vector<int>::iterator> TransformIterator;
class Derived : public Base<TransformIterator>
{
public:
Derived() : ints{132, 3, 6451, 12, 5} {}
TransformIterator begin()
{
return boost::make_transform_iterator(ints.begin(), toString());
} …
Run Code Online (Sandbox Code Playgroud) 我正在审查boost单元库,我很困惑为什么boost :: units :: unit类有一个额外的模板参数.这是一个例子:
http://www.boost.org/doc/libs/1_57_0/boost/units/unit.hpp
template<class Dim,class System, class Enable>
class unit
{
public:
typedef unit<Dim, System> unit_type;
typedef unit<Dim,System> this_type;
typedef Dim dimension_type;
typedef System system_type;
unit() { }
unit(const this_type&) { }
//~unit() { }
this_type& operator=(const this_type&) { return *this; }
// sun will ignore errors resulting from templates
// instantiated in the return type of a function.
// Make sure that we get an error anyway by putting.
// the check in the destructor.
#ifdef …
Run Code Online (Sandbox Code Playgroud) c++ templates generic-programming boost-units template-classes
我正在尝试用模板和SFINAE制作一些东西,我是初学者.我浪费了大量的时间来完成最简单的工作.你能帮我理解它的工作原理吗?
C <T,Ts ...>的构造函数采用T参数,该参数是A <U>或B <U>,但在这两种情况下具有不同的行为.我无法告诉你我试图这么做的一切.这对我来说似乎是最不愚蠢的.
template<typename T> class A{
public: A(){} };
template<typename T> class B{
public: B(){} };
template<typename T> struct enable_if_A {};
template<typename T> struct enable_if_A< A<T> > {typedef A<T> type;};
template<typename T> struct enable_if_B {};
template<typename T> struct enable_if_B< B<T> > {typedef B<T> type;};
template<typename T,typename... Ts> class C{
public:
C(typename enable_if_A<T>::type const &p){cout << "A" << endl;}
C(typename enable_if_B<T>::type const &p){cout << "B" << endl;}
};
// ...
A<float> a;
B<float> b;
C<A<float> > ca(a); …
Run Code Online (Sandbox Code Playgroud) c++ ×10
template-classes ×10
templates ×4
c++11 ×3
constructor ×2
boost-units ×1
c ×1
class ×1
for-loop ×1
g++ ×1
inheritance ×1
iterator ×1
name-lookup ×1
sfinae ×1