在您看来,学习和使用元编程的最佳语言(在简单性,可读性和代码优势方面)是什么?
我认为元编程是"编码的未来".不是说代码会灭绝,但我们可以看到这种情况会出现在新技术上.
我遇到了这个片段
template <typename T, size_t N>
char (&ArraySizeHelper(T (&array)[N]))[N];
#define arraysize(array) (sizeof(ArraySizeHelper(array)))
Run Code Online (Sandbox Code Playgroud)
在这篇文章http://software.intel.com/en-us/articles/pvs-studio-vs-chromium/
我见过其他模板做同样的事情,比如这个
我理解这些,但我一直遇到这个困难.
任何帮助,将不胜感激.
大多数C++编译器允许禁用异常.有没有办法从代码中确定它而不使用特定于编译器的预处理器宏,例如MSVC的_CPPUNWIND?理想情况下在编译时.
我想知道在编译时获取可变参数模板类的第N个参数的最简单和更常见的方法是什么(返回值必须作为编译器的静态const才能进行一些优化).这是我的模板类的形式:
template<unsigned int... T> MyClass
{
// Compile-time function to get the N-th value of the variadic template ?
};
Run Code Online (Sandbox Code Playgroud)
非常感谢你.
编辑:由于MyClass将包含200多个函数,我无法专门化它.但我可以在MyClass中专门化一个结构或函数.
编辑:从经过验证的答案得出的最终解决方案:
#include <iostream>
template<unsigned int... TN> class MyClass
{
// Helper
template<unsigned int index, unsigned int... remPack> struct getVal;
template<unsigned int index, unsigned int In, unsigned int... remPack> struct getVal<index, In,remPack...>
{
static const unsigned int val = getVal<index-1, remPack...>::val;
};
template<unsigned int In, unsigned int...remPack> struct getVal<1,In,remPack...>
{
static const unsigned int val = In; …Run Code Online (Sandbox Code Playgroud) 我想重新定义一个方法,但避免与之相关的警告.我应该使用undef_method或remove_method这样做吗?
(是的,重新定义方法有点hacky.我这样做是因为我在运行单元测试时有一些我想要使用的memoization,但是当程序本身运行时却没有.)
我整个早上都遇到过这个问题而没有任何结果.基本上,我需要一个简单的元编程事物,如果传递的参数是一种std :: vector,我可以分支到不同的特化.
某种类型的is_base_of用于模板.
这样的事情存在吗 ?
如果完全定义了类型,是否可以使用SFINAE进行检查?
例如
template <class T> struct hash;
template <> struct hash<int> {};
// is_defined_hash_type definition...
enum Enum { A, B, C, D };
static_assert ( is_defined_hash_type<int> ::value, "hash<int> should be defined");
static_assert (! is_defined_hash_type<Enum>::value, "hash<Enum> should not be defined");
Run Code Online (Sandbox Code Playgroud)
解决方案不应该修改哈希结构.
我一直在搞乱python的枚举库,并遇到了一个难题.在文档中,它们显示了一个自动编号枚举的示例,其中定义了一些内容:
class Color(AutoNumber):
red = ()
green = ()
...
Run Code Online (Sandbox Code Playgroud)
我想创建一个类似的类,但是该值将自动从成员的名称设置并保留从执行str和mixin中获得的功能enum
所以类似于:
class Animal(MagicStrEnum):
horse = ()
dog = ()
Animal.dog == 'dog' # True
Run Code Online (Sandbox Code Playgroud)
我已经查看了枚举模块的源代码,并尝试了很多变化__new__和EnumMeta类
我目前正在做这个技巧,以获得基于类型的cstring:
template<class ListT> static char constexpr * GetNameOfList(void)
{
return
std::conditional<
std::is_same<ListT, LicencesList>::value, "licences",
std::conditional<
std::is_same<ListT, BundlesList>::value, "bundles",
std::conditional<
std::is_same<ListT, ProductsList>::value, "products",
std::conditional<
std::is_same<ListT, UsersList>::value, "users",
nullptr
>
>
>
>;
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码不是很好看,如果我们想检查更多类型,这可能是不可读的.这是一种做同样的事情的方式,就像有一个开关盒块?
实际上,代码比这更复杂,因为std :: conditional需要一些类型,所以我们需要一些类来做这个技巧:
struct LicenceName { static char constexpr * value = "licences"; };
Run Code Online (Sandbox Code Playgroud)
例如.
考虑下一个代码示例:
template <typename... TArgs>
void foo(std::function<void(TArgs...)> f) {
}
template <typename... TArgs>
class Class {
public:
static void foo(std::function<void(TArgs...)> f) {
}
};
Run Code Online (Sandbox Code Playgroud)
为什么我可以这样做:
int main() {
// Helper class call
Class<int, int>::foo(
[](int a, int b) {}
);
}
Run Code Online (Sandbox Code Playgroud)
但这样做时出现编译错误:
int main() {
// Function call
foo<int, int>(
[](int a, int b) {}
);
}
Run Code Online (Sandbox Code Playgroud)
<source>:16:5: error: no matching function for call to 'foo'
foo<int, int>(
^~~~~~~~~~~~~
<source>:4:6: note: candidate template ignored: could not match
'std::function<void (int, int, TArgs...)>' …Run Code Online (Sandbox Code Playgroud)