我试图想办法循环一个模板参数列表,但没有成功
我不能使用c ++ 11可变参数模板功能,它需要在编译时完成
我可以假设在否定之后将没有积极的论据
任何的想法 ?
template< int F1, int F2 ,int F3>
struct TemplatedClass
{
TemplatedClass();
update()
{
/*
for each positive template argument
call a method
*/
}
};
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的对象类型:
struct T
{
int x;
bool y;
};
Run Code Online (Sandbox Code Playgroud)
和他们的容器像这样:
std::vector<T> v;
Run Code Online (Sandbox Code Playgroud)
和强烈的愿望来决定- 在单个语句 -任何元素是否v有y == true.这可能涉及到std::find_if.
我的理解是,std::bind并且boost::bind适用于成员函数,不能应用于成员数据.
因为我不喜欢他们,我希望避免:
因为我的环境是C++ 03,所以以下内容不可用:
对于C++ 03,标准说,在&&运算符的左右操作数之间有一个序列点,因此左操作符的所有副作用都发生在访问右操作符之前.
所以
int i = 0;
if (++i && i--)
std::cout << i;
Run Code Online (Sandbox Code Playgroud)
定义明确,保证输出0.
但是这个问题是什么:只有左操作数不是,才评估右操作数0?它似乎是一个细节,但对我来说,标准只保证操作数之间的序列点,而不是右操作数永远不会依赖于左操作数进行评估/访问.
例如
int arr[10];
int pos; // somehow set to a value from 0 to 10
while (pos < 10 && arr[pos] != 0)
pos++;
Run Code Online (Sandbox Code Playgroud)
这个定义得很好吗?pos可能是从开始10或到达10.左操作数没有副作用,与右操作数一致.我arr[10] != 0有从未履行的保证吗?
编辑:
感谢评论和回答,现在很清楚:
5.14p2: "The result is a bool. If the second expression is evaluated,
every value computation …Run Code Online (Sandbox Code Playgroud) c++ operator-precedence undefined-behavior sequence-points c++03
我正在嵌入式系统上编写应用程序.我需要某种类型的关联容器才能根据字符串访问某个指针.目前我正在使用地图(即 std::map<char*, SomeType*, CustomComparator>).我使用char*作为键类型,因为我正在与其他必须支持std :: string的库进行通信,而且我不想继续来回转换.
但我遇到了一个意想不到的问题.std :: map的每个实例(即新类型)在生成的二进制文件中占用大约10Kb(没有优化,用于调试目的).由于我限制了大约500Kb的ROM并且我可能需要几十个,这是一个非常显着的缺点(我想在实际硬件上进行调试,如果程序不适合我不能) .如果可能的话,我希望能够以对数时间访问这些项目.我不想求助于使用数组并循环遍历每个项目,直到找到我正在寻找的那个.插入不必很快,因为这只是在启动时完成的.删除仅在关闭期间执行,因此它们也不是问题.
有没有人有我可以使用的替代想法(最好是STL)?
注意:我仅限于C++ 03.
当然,当在该范围(ref)中直接知道内部函数调用时,C++编译器可以内联函数模板中的函数调用.
#include <iostream>
void holyheck()
{
std::cout << "!\n";
}
template <typename F>
void bar(F foo)
{
foo();
}
int main()
{
bar(holyheck);
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我传入holyheck一个类,它存储函数指针(或等效的),然后调用它?我有希望得到内联吗?怎么样?
template <typename F>
struct Foo
{
Foo(F f) : f(f) {};
void calledLater() { f(); }
private:
F f;
};
void sendMonkeys();
void sendTissues();
int main()
{
Foo<void(*)()> f(sendMonkeys);
Foo<void(*)()> g(sendTissues);
// lots of interaction with f and g, not shown here
f.calledLater();
g.calledLater();
}
Run Code Online (Sandbox Code Playgroud)
我的类型Foo旨在隔离大量的逻辑; 它将被实例化几次.调用的特定函数calledLater是实例化之间唯一不同的东西(尽管它在a的生命周期中永远不会改变 …
我有一个traits类,它应该只提供一个关于其他类型的信息(以字符串的形式):
template<typename T>
struct some_traits {
static const char* const some_string;
};
Run Code Online (Sandbox Code Playgroud)
我需要some_string为每种类型提供特殊实例.我知道如何执行此操作的常用方法是仅声明some_traits,然后编写特化:
template<typename T>
struct some_traits;
template<>
struct some_traits<blah> {
static const char* const some_string;
};
const char* const some_traits<blah>::some_string = "blah string";
Run Code Online (Sandbox Code Playgroud)
然而,当我需要的只是一个专门的代码时,这是很多代码some_string.有没有办法简化这个?
我试图摆弄明确的特化,但未能提出一种语法,不会让编译器向我的脸部吐出有毒的错误信息.
笔记:
我在课堂上有一个模板方法:
template<class T>
A& doSomething(T value)
Run Code Online (Sandbox Code Playgroud)
然后我有一个实现
template<class T>
A& A::doSomething(T value){
// do something with value
return *this;
}
Run Code Online (Sandbox Code Playgroud)
然后,我有一个专业,让我们说bool
template<>
A& A::doSomething(bool value){
// do something special with value of type bool
return *this
}
Run Code Online (Sandbox Code Playgroud)
这是我的理解,现在在代码中有类似的东西我不知道是什么意思:
template A& A::doSomething(char const*);
template A& A::doSomething(char);
template A& A::doSomething(int);
template A& A::doSomething(int*);
template A& A::doSomething(double);
...
Run Code Online (Sandbox Code Playgroud)
具有模板的最后5行的确切含义是什么?
谢谢
我试图使用pre-C++ 11静态断言.我找到了这个和这个问题,但不知怎的,我不能让它运行:
#define STATIC_ASSERT(x) \
do { \
const static char dummy[(x)?1:-1] = {0};\
} while(0)
struct bar {
int value;
template<typename T> void setValue(T x);
};
template<typename T> void bar::setValue(T x) { STATIC_ASSERT(1==0); }
template<> void bar::setValue(int x) { value = x;}
int main(){
bar b;
int c = 1;
b.setValue(c);
}
Run Code Online (Sandbox Code Playgroud)
编译此(gcc)会导致
错误:数组'dummy'的大小为负数
我希望只有当我setValue用除了以外的任何东西打电话时才会出现这个错误int.我也尝试了其他提出的解决方案,但结果大致相同:即使我没有用除了以外的任何东西实例化模板,也会出现错误int.我究竟做错了什么?
我在贸易公司工作,在这里延迟很重要。分配给我的项目是使用c和c ++ 98部分混合开发的,但是我相信我们可以使用C ++ 11进行相同的项目而不会降低效率。正如我与年长者讨论的那样,他们说您应该坚持使用C和c ++ 03,因为它们在微观层面上比C ++ 11更有效。谁能强调我?如果我使用C ++ 11,我会得到更好的结果吗?
为什么我需要在线*上制作checker指针
template <typename C> static yes test( checker<C, &C::helloworld>* );
为了使编译时间扣除正常工作,输出1 0?
当我删除时*,输出是0 0
#include <iostream>
struct Generic {};
struct Hello
{ int helloworld() { return 0; } };
// SFINAE test
template <typename T>
class has_helloworld
{
typedef char yes;
typedef struct {char _[2];} no;
template <typename C, int (C::*)()> struct checker;
template <typename C> static yes test( checker<C, &C::helloworld>* );
template <typename C> static no test(...);
public:
enum { …Run Code Online (Sandbox Code Playgroud) c++ ×10
c++03 ×10
templates ×5
boost ×1
boost-bind ×1
c++11 ×1
c++98 ×1
containers ×1
embedded ×1
gcc ×1
low-latency ×1
sfinae ×1
stl ×1
type-traits ×1