v
下面的示例代码中的变量是否已使用?
extern void* v;
template<void*&>
void f() {}
int main()
{
f<v>();
}
我在Boost ML中找到了这种模式。
cf. http://lists.boost.org/Archives/boost/2011/04/180082.php
它说boost::enabler
从未定义过,但是如果提供了选项,clang会将其拒绝为链接错误-g
。
cf. http://melpon.org/wandbox/permlink/nF45k7un3rFb175z
上面的示例代码是Boost ML代码的简化版本,clang也拒绝了它。
cf. http://melpon.org/wandbox/permlink/ZwxaygXgUhbi1Cbr
我认为,(但我不知道),以供参考类型模板非类型参数是ODR使用的,即使他们没有在他们的模板正文所以升压是形成不良的ML的图案简称。
我的理解正确吗?
[basic.def.odr] / 3:
一种可变
x
名字显示为一个潜在的评估表达式ex
是ODR使用的由ex
除非施加左值到右值转换(4.1),以x
产率常量表达式(5.19) [..]
不幸的是,此时将ltr转换应用于v
不会产生常数表达式-[expr.const] / 2:
甲条件表达式
e
是一个核心常量表达式除非的评价e
,如下所述抽象机(1.9),将评估下面的表达式中的一个的规则:[..]—左值到右值转换(4.1),除非将其应用于
整数或枚举类型的非易失性glvalue,它引用具有先前初始化,使用常量表达式[..]初始化的非易失性const对象,或
非易失性glvalue ,它引用用定义的非易失性对象
constexpr
,或引用该对象的不可更改子对象,或者- 文字类型的非易失性glvalue ,指的是其寿命在的求值内开始的非易失性对象
e
。
但是,尽管Matt建议的实现方法不正确,但是这个想法肯定是正确的。在此答案中使用助手模板演示了使用此方法的简单方法。根据您的情况,尝试
template <bool cond, int id=0>
using distinct_enable_if =
typename std::enable_if<cond, std::integral_constant<int,id>*>::type;
class test
{
public:
template< class... T,
distinct_enable_if<sizeof...(T) == 10> = nullptr>
test( T&&... ) {}
template< class T,
distinct_enable_if<std::is_arithmetic<T>{}> = nullptr>
operator T() const { return T{}; }
/* Note the additional template argument:
It ensures that the template parameter lists are not identical,
and the ODR isn't violated */
template< class T,
distinct_enable_if<std::is_pointer<T>{}, 1> = nullptr>
operator T() const { return T{}; }
};
演示。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句