见下面的例子:
int arr[10];
int *p = arr; // 1st valid choice
int (&r)[10] = arr; // 2nd valid choice
Run Code Online (Sandbox Code Playgroud)
现在,当我们用auto反对arr的话,它选择的第一选择.
auto x = arr; // x is equivalent to *p
Run Code Online (Sandbox Code Playgroud)
它有可能吗?我希望它能够启用参数的编译时传递.假设它只是为了方便用户,因为人们可以随时输出真实类型template<class T, T X>,但是对于某些类型,即指向成员函数的指针,即使使用decltype快捷方式,它也非常繁琐.请考虑以下代码:
struct Foo{
template<class T, T X>
void bar(){
// do something with X, compile-time passed
}
};
struct Baz{
void bang(){
}
};
int main(){
Foo f;
f.bar<int,5>();
f.bar<decltype(&Baz::bang),&Baz::bang>();
}
Run Code Online (Sandbox Code Playgroud)
是否有可能将其转换为以下内容?
struct Foo{
template<auto X>
void bar(){
// do something with X, compile-time passed
}
};
struct Baz{
void bang(){
}
};
int main(){
Foo f;
f.bar<5>();
f.bar<&Baz::bang>();
}
Run Code Online (Sandbox Code Playgroud) 假设我们有以下类型
template <bool... Values>
struct foo{};
Run Code Online (Sandbox Code Playgroud)
我想从constexpr数组创建一个可变参数模板bool tab[N].换句话说,我想做的事情如下:
constexpr bool tab[3] = {true,false,true};
using ty1 = foo<tab[0], tab[1], tab[2]>;
Run Code Online (Sandbox Code Playgroud)
但我想以编程方式进行.现在,我尝试了以下内容:
template <std::size_t N, std::size_t... I>
auto
mk_foo_ty(const bool (&tab)[N], std::index_sequence<I...>)
{
// error: template argument for template type parameter must be a type
return foo<tab[I]...>{};
}
// error (see mk_foo_ty)
using ty2 = decltype(mk_ty(tab, std::make_index_sequence<3>{}));
// error: expected '(' for function-style cast or type construction
using ty3 = foo<(tab[std::make_index_sequence<3>])...>;
Run Code Online (Sandbox Code Playgroud)
我甚至不确定它是否可能.也许诉诸于像Boost.Preprocessor这样的东西,但我不喜欢这个想法.那么,有没有人有想法?谢谢!
编辑
我一边是constexpr布尔方形矩阵的框架,可以在编译时使用xor,否定等创建.
另一方面,我有一个模板框架,它使用布尔值作为参数,使用在可变参数模板中编码的信息静态创建操作.
我的目标是弥合这两个框架之间的差距.因此,我无法使用硬编码解决方案. …