我必须专门化模板如果他们的罪行代码是在if(假) [英] Do I Have to Specialize Templates If Their Offending Code Is in an if(false)

查看:208
本文介绍了我必须专门化模板如果他们的罪行代码是在if(假)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定层次结构:

struct base {};
struct a : public base {};
struct b : public base {};

我想填写 vector< base *> vecBase 向量< a *> aVec 具有此功能:

I want to fill vector<base*> vecBase and vector<a*> aVec with this function:

template <typename T>
void foo(T* bar) {
    if (is_base_of_v<decltype(baseVec)::value_type, T>) baseVec.push_back(static_cast<decltype(baseVec)::value_type>(bar));
    if (is_base_of_v<decltype(aVec)::value_type, T>) baseVec.push_back(static_cast<decltype(aVec)::value_type>(bar));
}        

这里的问题是,即使 static_cast 将永远不会执行,除非它是合法的;这样的调用无法编译:

The problem here is that even though the static_cast will never be performed unless it's legal; calls like these fail to compile:

int myInt;
b myB;

foo(&myInt);
foo(&myB);

我知道我可以专门化 foo 。这是我在这里要做的,还是有一种方法提示编译器的事实,冒犯的 static_cast 将永远不会发生?

I know that I can specialize foo. Is that what I have to do here, or is there a way to tip the compiler off to the fact that the offending static_casts will never happen?

推荐答案


  1. static if 是您要找的。它最初是由 1月10日的N3329提出的, '12

在3月16日 - 13日Bjarne Stroustrup,Gabriel Dos Reis和Andrew Sutton提出N3613 ,其中表示 static if




。解决这些问题的语言特性不得对语言和我们构建工具的能力产生负面影响。我们得出结论, static if 的未来发展应该放弃,而应该采用诸如concepts-lite方法的替代方法。

This proposal would do much more harm than good. Language features addressing these problems must not negatively affect the language and our ability to build tools around it. We conclude that future development of static if should be abandoned, and that alternatives such as "concepts-lite" approach should be pursued instead.




  1. 概念研究组于2013年9月23日召开的C ++芝加哥会议指出, =http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4421.html#29 =nofollow>未包含 static if

  1. The C++ Chicago Meeting on Sep-23-'13 the Concepts Study Group stated that they weren't including static if in their scope for the near future.

芝加哥会议确实催生了Ville Voutilainen的简化 static if http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4461.html

The Chicago Meeting did spawn Ville Voutilainen's simplification of static if: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4461.html

5月4日至15日的C + + Lexena会议讨论了简化和鼓励原创作者回到10月19日 - 15日的C ++ Kona会议

The C++ Lexena Meeting on May-4-'15 did discuss the simplification and encouraged the original authors to come back to the C++ Kona meeting on Oct-19-'15

C ++ Kona会议 Herb Sutter亲自提出 static if 。他讲述了

In the C++ Kona Meeting Herb Sutter personally presented static if. He recounts that,




房间里的反馈甚至是Bjarne从它的角度来看:这可能是将一个版本的概念/概念精简到C + + 17的唯一方法。没有其他方法我们会在这个标准的主要版本中得到Concept或任何类似的东西。所以这是早期的日子,但它已经提出。有一些兴趣。我们现在需要跟进,这些事情需要时间和几个周期,但它是建议。

The feedback in the room, even Bjarne, was potentially interested in it from the point of view: This may be the only way to get a version of Concepts/Concepts Lite into C++17. There's no other way we'll get Concepts or anything like it into this major revision of the standard. So this is early days yet, but it has been presented. There is some interest. We now need to follow up and these things do take time and several cycles, but it is being proposed.






但是现在你必须专门化你的模板:


But for now you're going to have to specialize your templates:

template <typename T>
enable_if_t<negation_v<is_base_of<base, T>>, void> push(T*) {}

void push(base* i) {
    baseVec.push_back(static_cast<decltype(baseVec)::value_type>(i));
}

void push(a* i) {
    baseVec.push_back(static_cast<decltype(baseVec)::value_type>(i));
    aVec.push_back(static_cast<decltype(aVec)::value_type>(i));
}

这篇关于我必须专门化模板如果他们的罪行代码是在if(假)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆