這是very natural想要比較std::array
在編譯時;其operator==()
明顯是constexpr
'能夠。然而 - 它isn't標記爲constexpr
。這是故意的還是疏忽?而且 - 這是什麼原因(顯然在C++ 17中)?爲什麼不是std :: array的operator ==()標記爲constexpr?
回答
P0031解釋爲什麼它沒有提出constexpr
比較:
目前比較和
swap
/fill
可以與算法的幫助 從<algorithm>
頭實現。與 標記比較constexpr將打破該能力,並可能導致性能下降 。
例如,==
可以在std::equal
方面來實現,其中 - 在適當的情況下 - 可以調用高度優化 - 丁決然-不可─constexpr
memcmp
。在==
上編制constexpr
將排除此優化,而無需特殊的編譯器幫助。
基本原理可能是這樣的:如果包含的類型的==
也是constexpr
,則陣列的==
只能是constexpr
。
由於容器無法執行該操作,因此它不能(通常)提供operator==() constexpr
。
對於任何模板參數做任何事情的模板化constexpr函數,第一句都是正確的。而且 - 這些可以很好地做成「constexpr」......所以不是這樣。 (PS - 不是我的downvote。) – einpoklum
我很難想到一個模板化的constexpr函數,值得這個名字,並處理非constexpr類型的成員... –
- 1. 爲什麼std :: chrono :: system_clock :: to_time_t()不是constexpr?
- 2. 什麼時候是std :: string_view :: operator ==的確是constexpr?
- 3. 爲什麼不std :: weak_ptr有operator->?
- 4. 爲什麼std :: vector和std :: array的C++ initializer_list行爲不同?
- 5. 爲什麼std :: forward放棄了constexpr- ness?
- 6. 爲什麼std :: vector需要operator =
- 7. 爲什麼constexpr不是自動的?
- 8. 爲什麼std :: array < T, 0 >不是空的?
- 9. 爲什麼需要用std :: string :: operator +()顯式調用Myclass :: operator string()?
- 10. 爲什麼C++ 11 std :: array結構而不是類?
- 11. 爲什麼std :: stoi和std :: array不能用g ++ C++ 11編譯?
- 12. 爲什麼std :: is_array爲std :: array返回false?
- 13. 即使成員函數是constexpr,爲什麼需要constexpr?
- 14. 爲什麼不能在std :: string上重載「operator <<」?
- 15. C++:爲什麼'operator + ='定義了字符串而不是'operator +'?
- 16. 爲什麼不是vector :: operator []實現類似於map :: operator []?
- 17. 爲什麼M :: operator <<導致鏈接錯誤,而不是std :: cout :: operator <<
- 18. $ array [] = new stdClass;爲什麼array []而不是array
- 19. 爲什麼&array!=&array [0]?
- 20. 爲什麼std :: array <int, 10> x不是零初始化的,而是std :: array <int, 10> x = std :: array <int, 10>()似乎是?
- 21. 爲什麼std :: hash不是專用於std :: reference_wrapper的?
- 22. 爲什麼sfinae如果constexpr不允許?
- 23. 爲什麼是std ::加上類模板?
- 24. 爲什麼未指定std :: reverse_iterator :: operator []的返回類型?
- 25. 爲什麼{}用於訪問std :: hash中的operator()?
- 26. 'array'不是'std'的成員
- 27. 爲什麼不是std :: string是std :: vector的專門化?
- 28. 爲什麼重載operator()?
- 29. 爲什麼HttpUtility.UrlPathEncode標記爲「不使用」?
- 30. 爲什麼std :: mutex :: unlock()不是noexcept
丹尼爾克魯格說:這可能會解決何時/如果提案通過[P0202](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0202r1.html)。 – einpoklum