我在c ++中搜索扩展方法的实现,并且在这个comp.std.c ++讨论中提到了polymorphic_map可以用于与类关联的方法,但是,提供的链接似乎已经死了.有没有人知道答案所指的是什么,或者是否有另一种方法以类似的方式扩展类到扩展方法(可能通过一些mixins的使用?).
我知道规范的C++解决方案是使用自由函数; 这更多是出于好奇而不是其他任何事情.
我希望以下代码可以工作,但我收到了一个编译错误:
Run Code Online (Sandbox Code Playgroud)error C2975: 'n' : invalid template argument for 'foo', expected compile-time constant expression
#include <iostream>
using namespace std;
template<int N>
struct foo
{
foo() { cout << N << endl; }
};
int main()
{
foo< __LINE__ > f;
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样?我__LINE__会在模板实例化之前粘贴行号吗?
如果我想这样做,我应该引入一个static const int来保存行号或是否有标准解决方案?