可以为促销活动制作类型特征吗?

时间:2013-11-09 12:14:58

标签: c++ c++11 type-promotion

我需要知道促销何时发生,以及它是什么。我猜是

template <typename T>
struct promoted { using type = std::common_type_t<T, T>; };

template <typename T>
using promoted_t = typename promoted<T>::type;

显然,如果用户开始覆盖std::common_type的版本,这将会中断。假设没有发生,它会起作用吗?条件运算符应该在进一步评估之前应用促销。我认为有一天这样的事情应该在标准中。

如果你想知道为什么我想要这个,那就是C级别的变种:

auto  r = va_arg( the_va_list, T );

如果我最初传入的类型在varargs中使用时会被转换,例如float变为double s,我是否会为T添加原始类型,或者破损型?如果是后者,我会为此制作特征类型,这需要在最后一步中提升特征。

1 个答案:

答案 0 :(得分:0)

粗略地说,bool ? T : T生成T。在那个表达中没有任何东西得到提升。

在C风格的可变参数列表中传递非POD(或实际上,任何用户定义的)类型会调用未定义的行为。

例如,MSC将结构的副本推送到堆栈而不调用构造函数(如果定义了构造函数)。

例如:

struct thing
{
    int v;

    // Not called
    operator int() const throw()
    {return this->v;}

    thing(int v) throw() :
    v(v)
    {}

    // Also not called
    thing(const thing &other) throw() :
    v(other.v)
    {}
};

void frob(...)
{
}

int main(int argc, const char **argv)
{
    thing t(47);

    frob(t); // <- rep stosd/q here, but this is really UD

    return 0;
}

编辑:

澄清一下:不可能使用模板来检测传递给C风格的可变参数列表的内容,因为在这种情况下编译器对用户定义的类型所做的事情是未定义的。

相关问题