hal*_*elf 5 c++ clang language-lawyer clang++ c++17
我在 x64 Linux 上使用 clang 9.0.1 和 gcc 9.2.1,两者都带有--std=c++17(或--std=c++2a)。
Gcc 可以在没有任何错误或警告的情况下构建以下示例,而 clang++ 会报告error: constexpr if condition is not a constant expression这两if constexpr行。(顺便说一句,在我的 MacBook 上,Apple clang-11.0.0 也报告了同样的错误。)
MCVE:
#include <utility>
enum class TopicType {
MarketData = 'M',
Timer = 'T',
};
template<class Topic>
struct TopicBase {
constexpr static TopicType type() { return Topic::type; };
const Topic& topicImp;
explicit TopicBase(const Topic &t) : topicImp(t) {}
};
struct MarketDataTopic {
static constexpr TopicType type{TopicType::MarketData};
};
struct TimerTopic {
static constexpr TopicType type{TopicType::Timer};
};
template<class Topic>
int subscribe(TopicBase<Topic>&& topic) {
if constexpr (topic.type() == TopicType::MarketData) { // <-- clang++ reports error here
return 1;
}
if constexpr (topic.type() == TopicType::Timer) { // and error here
return 2;
}
return -1;
}
int main(int argc, const char **argv) {
MarketDataTopic m{};
TimerTopic t{};
TopicBase<MarketDataTopic> b1{m};
TopicBase<TimerTopic> b2{t};
subscribe(std::move(b1));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在线编译器https://godbolt.org/z/rARi_N有相同的结果。
那么哪个编译器是正确的呢?如果它是一个错误,如何修复它的叮当声?
那么你可以这样做以使其适用于 clang:
template<class Topic>
int subscribe(TopicBase<Topic>&& topic) {
using myType = TopicBase<Topic>;
if constexpr (myType::type() == TopicType::MarketData) { // <-- clang++ reports error here
return 1;
}
if constexpr (myType::type() == TopicType::Timer) { // and error here
return 2;
}
return -1;
}
Run Code Online (Sandbox Code Playgroud)
这很可能是 clang 中的一个错误。最好举报一下
| 归档时间: |
|
| 查看次数: |
326 次 |
| 最近记录: |