You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Подходит к концу 2019 год. Уже скоро C++20 станет доступен во всей своей красе. Но совсем не скоро он плотно войдет в мир промышленной разработки (сюда еще даже C++14 не везде дошел).
В С++20 появятся концепции/типажи (concepts) -- долгожданная горсть синтаксического сахара, призванная дать разработчикам возможность писать шаблоны (templates), накладывая ограничения на их параметры так, чтоб потом не было мучительно больно.
В предыдущей серии
мы изобретали нечто, называемое XXX_traits, где под XXX скрывается какой-то концепт. А сам трейт описывает, какие операции доступны для типов, реализующих данный концепт. И все взаимодействие с шаблонными параметрами происходило с помощью вспомогательной структуры.
в предыдущей серии мы изобретали CRTP.
А в этой серии мы вернемся обратно к тому с чего начинали и изобретем удивительно прекрасную синтаксическую конструкцию, сравнимую по красоте с концептами.
Пусть наша задача написать свой собственный std::bit_cast. C++20, конечно, вышел, но до продакшена везде и повсюду ему далеко, так что такая задача вполне может быть.
bit_cast позволяет посмотреть на значение одного типа, как будто бы это значение совершенно другого типа. Как reinterpret_cast, только без нарушения strict aliasing rule и провоцирования UB.
В простейшем и очень не правильном виде, bit_cast реализуется так: