我正在寻找别的东西与模板模板参数和偶然发现这个答案谓其模板模板模板参数不标准允许.
但是,以下代码在最新的clang(3.2)和最新的GCC(4.8)中编译:
template<template<template<typename> class> class T> struct test {};
template<template<typename> class T> struct foo {};
test<foo> bar;
Run Code Online (Sandbox Code Playgroud)
这是一个扩展,还是其他答案实际上是错误的并且标准允许?如果没有,是否有任何特殊原因遗漏?
我正在创建一个模板类,它将类型转换为描述它的字符串,例如typeinfo<int(*)()>::name()返回字符串"int(*)()"(最多为空格).最初我有大量的特殊情况来解决这个事实,即typeid(...).name()剥离引用限定符和顶级cv限定符,但后来我记得将类型作为模板参数传递将保留这些.所以,使用ABI标头,我最终得到了这样的东西:
#include <iostream>
#include <typeinfo>
#include <string>
#include <cxxabi.h>
using namespace std;
string demangle(const char* mangledName) {
int status;
char* result = abi::__cxa_demangle(mangledName, nullptr, nullptr, &status);
switch(status) {
case -1:
cerr << "Out of memory!" << endl;
exit(1);
case -2:
return mangledName;
case -3: // Should never happen, but just in case?
return mangledName;
}
string name = result;
free(result);
return name;
}
template<typename T> struct preserve_qualifiers {};
template<typename T> class typeinfo {
using …Run Code Online (Sandbox Code Playgroud) 不久之前,我对"参数化"用户定义的文字有一个想法,并想知道在当前的C++标准中是否有任何方法可以做到这一点.
基本上,我们的想法是拥有一个用户定义的文字,其行为可以根据一些参数进行调整.作为一个简单的例子,我选择了一个"定点"文字,它将浮点数转换为整数; 参数是小数位数的精度.
这只是一个练习,因为我不确定这在实际应用中是如何有用的.
我的第一个想法是这样的:
namespace fp_impl {
constexpr int floor(long double n) {
return n;
}
constexpr int pow10(int exp) {
return exp == 0 ? 1 : 10 * pow10(exp - 1);
}
template<int i>
constexpr int fixed_point(long double n) {
return floor(n * pow10(i));
}
namespace fp2 {
constexpr int operator"" _fp (long double n) {
return fixed_point<2>(n);
}
}
namespace fp4 {
constexpr int operator"" _fp (long double n) {
return fixed_point<4>(n);
}
}
}
template<int …Run Code Online (Sandbox Code Playgroud)