为什么这不能编译?(克++ - 4.5)
template < typename U >
static void h () {
}
int main () {
auto p = &h<int>; // error: p has incomplete type
}
Run Code Online (Sandbox Code Playgroud)
编辑:这是一个解决方法:
template < typename U >
static void h () {
}
int main () {
typedef decltype (&h<int>) D;
D p = &h<int>; // works
}
Run Code Online (Sandbox Code Playgroud) 虽然有很多不同的复杂的实现中malloc/ free的C/C++,我在寻找一个非常简单的和(特别是)小一个在一个固定大小的缓冲区和支持工作realloc.不需要螺纹安全等,并且我的物体很小并且尺寸变化不大.有没有可以推荐的实施方案?
编辑:
我将使用该实现在接收器处的通信缓冲区来传输具有可变大小的对象(接收器未知).分配的对象不会长寿,但可能同时使用多个对象.
由于每个人似乎都推荐标准的malloc,我或许应该重新提出我的问题.我需要的是在缓冲区之上的"最简单"的malloc实现,我可以根据自己的需要开始优化.也许最初的问题不清楚,因为我不是在寻找一个优化的malloc,只是为了一个简单的.我不想从一个glibc-malloc开始并扩展它,但是它的重量轻.
有没有任何开源工具或库可用于简单的自定义程序集类语言(用于自动生成的程序)的静态代码分析以及它们能够做什么(检测未使用的代码/寄存器,为代码段提供高级表达式,调用图形)等等.)?该领域确实存在哪些算法?
我有很多像这样的代码:
#define WITH_FEATURE_X
struct A {
#ifdef WITH_FEATURE_X
// ... declare some variables Y
#endif
void f ();
};
void A::f () {
// ... do something
#ifdef WITH_FEATURE_X
// ... do something and use Y
#else
// ... do something else
#endif
// ... do something
}
Run Code Online (Sandbox Code Playgroud)
我想用模板参数替换#defines:
template < int WITH_FEATURE_X > // can be 0 or 1
struct A;
Run Code Online (Sandbox Code Playgroud)
但我不想为A <0> :: f()和A <1> :: f()几乎复制A :: f()的整个代码,只是为了依赖于参数的几行.我也不想调用函数而不是之前的#ifdefs.什么是常见的解决方案?
我有两个c ++程序需要有一个type -> int在编译时已知并且在两个程序之间相同的映射.此外,我想在编译时自动确保地图是一对一的.你会如何解决这个问题?(允许使用c ++ 0x扩展名).第一部分很简单:分享一个
template < typename T > struct map;
template <> struct map <...> { enum { val = ...; }; };
Run Code Online (Sandbox Code Playgroud)
程序之间.(第二部分意味着我不想val在我的程序中的某个地方意外地为两种不同类型定义相同的内容.)
How do I fix this syntax error?
struct A {
template < typename T >
void f () {}
};
template < typename C, typename U >
struct B {
void g () {
U::f < C > (); // expected primary-expression before »>« token
}
};
int main () {
B<int,A> b;
b.g ();
}
Run Code Online (Sandbox Code Playgroud) 在对上一个问题的有用答案之后,我开始使用模板方法模式来创建具有许多不同选项的类.如果没有全部实现它们,那么我对该类对象的当前声明现在看起来像这样:
pc < prg, tc, 9, 0, 4, 4, test, true, true, true, true, false, true, true, 10, 0, -1, 3, 3 > mp;
Run Code Online (Sandbox Code Playgroud)
你如何处理长模板参数列表?我应该使用枚举/定义而不是真/假和数字吗?有常用的替代品吗?
我想复制并调用一个函数,但是在调用缓冲区时,下面的代码会出现段错误.我需要改变什么?(Linux,x86)
#include <string.h>
#include <malloc.h>
#include <stdio.h>
int foo () { return 12; }
void foo_end () {}
int main () {
int s = (unsigned long long) foo_end - (unsigned long long) foo;
int (*f) () = (int (*)()) malloc (s);
memcpy ((void*) f, (const void*) foo, s);
printf ("%d %d\n", f (), foo ());
}
Run Code Online (Sandbox Code Playgroud)
编辑:工作解决方案:
#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
int foo () { return 12; }
void foo_end () {} …Run Code Online (Sandbox Code Playgroud)