Skip to content

Latest commit

 

History

History
97 lines (53 loc) · 3.33 KB

CppNarrow_cast.md

File metadata and controls

97 lines (53 loc) · 3.33 KB

 

 

 

 

 

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

  • STL STL: GNU ISO C++ Library, version 4.9.2

 

 

 

 

 

Qt project file: ./CppNarrow_cast/CppNarrow_cast.pro

 


TEMPLATE = app CONFIG += console CONFIG -= app_bundle CONFIG -= qt QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++ -Werror SOURCES += main.cpp

 

 

 

 

 

./CppNarrow_cast/main.cpp

 


#include <cassert> #include <stdexcept> //Copied from // * Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. //   ISBN: 978-0-321-56384-2. Chapter 11.5: Explicit type conversion. page 299. template <class Target, class Source> Target narrow_cast(Source v) {   auto r = static_cast<Target>(v);   if (static_cast<Source>(r)!=v)     throw std::runtime_error("narrow_cast<>() failed");   return r; } int main() {   try   {     narrow_cast<int>(12.34);     assert(!"Incorrect conversion from double to int should be detected by narrow_cast");   }   catch (std::runtime_error& e)   {     //OK   } }