以下文章是我为模板参数包找到的第一个提案.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1603.pdf
在第16页,它讨论了引入两个新的运算符[]和<>来访问参数包元素和参数包类型.
The suggested syntax for such an operator involves two new operators: .[] to access values and .<> to access types. For instance:
template<int N, typename Tuple> struct tuple_element;
template<int N, ... Elements>
struct tuple_element<tuple<Elements...> >
{
typedef Elements.<N> type;
};
template<int N, ... Elements>
Elements.<N>& get(tuple<Elements...>& t)
{ return t.[N]; }
template<int N, ... Elements>
const Elements.<N>& get(const tuple<Elements...>& t)
{ return t.[N]; }
Run Code Online (Sandbox Code Playgroud)
那么这些运营商在哪里?如果没有,他们的替代品是什么?
在C++ 0x中,可以创建一个constexpr std :: tuple,例如
#include <tuple>
constexpr int i = 10;
constexpr float f = 2.4f;
constexpr double d = -10.4;
constexpr std::tuple<int, float, double> tup(i, f, d);
Run Code Online (Sandbox Code Playgroud)
还可以在运行时查询std :: tuple,例如通过
int i2 = std::get<0>(tup);
Run Code Online (Sandbox Code Playgroud)
但是在编译时无法查询它,例如,
constexpr int i2 = std::get<0>(tup);
Run Code Online (Sandbox Code Playgroud)
将抛出编译错误(至少使用最新的g ++快照2011-02-19).
有没有其他方法在编译时查询constexpr std :: tuple?
如果没有,是否有一个概念上的原因,为什么不应该查询它?
(我知道避免使用std :: tuple,例如,通过使用boost :: mpl或boost :: fusion,但不知何故,如果不在新标准中使用元组类,这听起来是错误的......).
顺便问一下,有人知道为什么
constexpr std::tuple<int, float, double> tup(i, f, d);
Run Code Online (Sandbox Code Playgroud)
编译好,但是
constexpr std::tuple<int, float, double> tup(10, 2.4f, -10.4);
Run Code Online (Sandbox Code Playgroud)
不?
非常感谢提前! - 啦
common_type<long, unsigned long>::type是unsigned long因为关于积分推广后的操作数标准说...
[...]如果具有无符号整数类型的操作数的秩大于或等于另一个操作数的类型的等级,则带有符号整数类型的操作数应转换为具有无符号整数类型的操作数的类型
不要调用整数提升系统错误,但似乎有一个更大的有符号整数类型,它可以表示应该使用的有符号和无符号操作数的范围.
我知道有些平台可能长==很长,在这种情况下上述规则可以生效.但如果是一个更大的符号整数类型可用,不应该应用它呢?
这里std::make_array提出的功能的当前状态是什么?我找不到有关其潜在接受度的任何信息.根据cppreference.com,它位于命名空间中.在C++编译器支持和Wikipedia-C++ 17,Wikipedia-C++ 20和C++ 17标准草案中都没有提到它.std::experimental
我一直在玩一套模板,用于在C++中给出两种原始类型来确定正确的促销类型.这个想法是,如果你定义一个自定义数字模板,你可以使用它们来确定,例如,基于传递给模板的类的操作符+函数的返回类型.例如:
// Custom numeric class
template <class T>
struct Complex {
Complex(T real, T imag) : r(real), i(imag) {}
T r, i;
// Other implementation stuff
};
// Generic arithmetic promotion template
template <class T, class U>
struct ArithmeticPromotion {
typedef typename X type; // I realize this is incorrect, but the point is it would
// figure out what X would be via trait testing, etc
};
// Specialization of arithmetic promotion template
template <>
class ArithmeticPromotion<long long, unsigned …Run Code Online (Sandbox Code Playgroud)