[{"data":1,"prerenderedAt":894},["ShallowReactive",2],{"doc-tostring":3},{"id":4,"title":5,"body":6,"description":114,"extension":886,"meta":887,"navigation":533,"path":888,"seo":889,"slug":890,"source":891,"stem":892,"__hash__":893},"docs\u002Fdocs\u002Ftostring.md","String conversions",{"type":7,"value":8,"toc":878},"minimark",[9,50,54,98,101,105,108,202,212,215,218,229,323,326,340,403,406,427,462,465,470,491,494,593,596,749,752,763,784,864,874],[10,11,12],"blockquote",{},[13,14,15,19,20,24,25,28,29,32,33,36,37,36,40,43,44,49],"p",{},[16,17,18],"strong",{},"Catch3 compatibility reference:"," This page describes the test-writing API Catch3 shares with Catch2: the macros, such as ",[21,22,23],"code",{},"TEST_CASE"," and ",[21,26,27],{},"REQUIRE",", and the ",[21,30,31],{},"Catch::"," namespace keep Catch2's names. Headers and build names are Catch3's own (",[21,34,35],{},"\u003CProgmasoft\u002FCatch3\u002FTestMacros.hpp>",", ",[21,38,39],{},"find_package(Catch3)",[21,41,42],{},"Catch3::Catch3WithMain","). See ",[45,46,48],"a",{"href":47},"\u002Fdocs\u002Fprogmasoft-catch3\u002F","Catch3 additions"," for Progmasoft APIs.",[51,52,5],"h1",{"id":53},"string-conversions",[13,55,56,59,62,66,68,72,74,78,80,84,86,90,92,96],{},[16,57,58],{},"Contents",[60,61],"br",{},[45,63,65],{"href":64},"#operator--overload-for-stdostream","operator \u003C\u003C overload for std::ostream",[60,67],{},[45,69,71],{"href":70},"#catchstringmaker-specialisation","Catch::StringMaker specialisation",[60,73],{},[45,75,77],{"href":76},"#catchis_range-specialisation","Catch::is_range specialisation",[60,79],{},[45,81,83],{"href":82},"#exceptions","Exceptions",[60,85],{},[45,87,89],{"href":88},"#enums","Enums",[60,91],{},[45,93,95],{"href":94},"#floating-point-precision","Floating point precision",[60,97],{},[13,99,100],{},"Catch needs to be able to convert types you use in assertions and logging expressions into strings (for logging and reporting purposes).\nMost built-in or std types are supported out of the box but there are two ways that you can tell Catch how to convert your own types (or other, third-party types) into strings.",[102,103,65],"h2",{"id":104},"operator-overload-for-stdostream",[13,106,107],{},"This is the standard way of providing string conversions in C++ - and the chances are you may already provide this for your own purposes. If you're not familiar with this idiom it involves writing a free function of the form:",[109,110,115],"pre",{"className":111,"code":112,"language":113,"meta":114,"style":114},"language-cpp shiki shiki-themes github-dark","std::ostream& operator \u003C\u003C ( std::ostream& os, T const& value ) {\n    os \u003C\u003C convertMyTypeToString( value );\n    return os;\n}\n","cpp","",[21,116,117,172,187,196],{"__ignoreMap":114},[118,119,122,126,130,133,137,140,143,146,148,150,152,154,158,160,163,166,169],"span",{"class":120,"line":121},"line",1,[118,123,125],{"class":124},"svObZ","std",[118,127,129],{"class":128},"s95oV","::",[118,131,132],{"class":124},"ostream",[118,134,136],{"class":135},"snl16","&",[118,138,139],{"class":135}," operator",[118,141,142],{"class":124}," \u003C\u003C",[118,144,145],{"class":128}," ( ",[118,147,125],{"class":124},[118,149,129],{"class":128},[118,151,132],{"class":124},[118,153,136],{"class":135},[118,155,157],{"class":156},"s9osk"," os",[118,159,36],{"class":128},[118,161,162],{"class":124},"T",[118,164,165],{"class":135}," const&",[118,167,168],{"class":156}," value",[118,170,171],{"class":128}," ) {\n",[118,173,175,178,181,184],{"class":120,"line":174},2,[118,176,177],{"class":128},"    os ",[118,179,180],{"class":135},"\u003C\u003C",[118,182,183],{"class":124}," convertMyTypeToString",[118,185,186],{"class":128},"( value );\n",[118,188,190,193],{"class":120,"line":189},3,[118,191,192],{"class":135},"    return",[118,194,195],{"class":128}," os;\n",[118,197,199],{"class":120,"line":198},4,[118,200,201],{"class":128},"}\n",[13,203,204,205,207,208,211],{},"(where ",[21,206,162],{}," is your type and ",[21,209,210],{},"convertMyTypeToString"," is where you'll write whatever code is necessary to make your type printable - it doesn't have to be in another function).",[13,213,214],{},"You should put this function in the same namespace as your type, or the global namespace, and have it declared before including Catch's header.",[102,216,71],{"id":217},"catchstringmaker-specialisation",[13,219,220,221,224,225,228],{},"If you don't want to provide an ",[21,222,223],{},"operator \u003C\u003C"," overload, or you want to convert your type differently for testing purposes, you can provide a specialization for ",[21,226,227],{},"Catch::StringMaker\u003CT>",":",[109,230,232],{"className":111,"code":231,"language":113,"meta":114,"style":114},"namespace Catch {\n    template\u003C>\n    struct StringMaker\u003CT> {\n        static std::string convert( T const& value ) {\n            return convertMyTypeToString( value );\n        }\n    };\n}\n",[21,233,234,245,253,269,296,306,312,318],{"__ignoreMap":114},[118,235,236,239,242],{"class":120,"line":121},[118,237,238],{"class":135},"namespace",[118,240,241],{"class":124}," Catch",[118,243,244],{"class":128}," {\n",[118,246,247,250],{"class":120,"line":174},[118,248,249],{"class":135},"    template",[118,251,252],{"class":128},"\u003C>\n",[118,254,255,258,261,264,266],{"class":120,"line":189},[118,256,257],{"class":135},"    struct",[118,259,260],{"class":124}," StringMaker",[118,262,263],{"class":128},"\u003C",[118,265,162],{"class":124},[118,267,268],{"class":128},"> {\n",[118,270,271,274,277,279,282,285,288,290,292,294],{"class":120,"line":198},[118,272,273],{"class":135},"        static",[118,275,276],{"class":124}," std",[118,278,129],{"class":128},[118,280,281],{"class":124},"string",[118,283,284],{"class":124}," convert",[118,286,287],{"class":128},"( ",[118,289,162],{"class":124},[118,291,165],{"class":135},[118,293,168],{"class":156},[118,295,171],{"class":128},[118,297,299,302,304],{"class":120,"line":298},5,[118,300,301],{"class":135},"            return",[118,303,183],{"class":124},[118,305,186],{"class":128},[118,307,309],{"class":120,"line":308},6,[118,310,311],{"class":128},"        }\n",[118,313,315],{"class":120,"line":314},7,[118,316,317],{"class":128},"    };\n",[118,319,321],{"class":120,"line":320},8,[118,322,201],{"class":128},[102,324,77],{"id":325},"catchis_range-specialisation",[13,327,328,329,24,332,335,336,339],{},"As a fallback, Catch attempts to detect if the type can be iterated\n(",[21,330,331],{},"begin(T)",[21,333,334],{},"end(T)"," are valid) and if it can be, it is stringified\nas a range. For certain types this can lead to infinite recursion, so\nit can be disabled by specializing ",[21,337,338],{},"Catch::is_range"," like so:",[109,341,343],{"className":111,"code":342,"language":113,"meta":114,"style":114},"namespace Catch {\n    template\u003C>\n    struct is_range\u003CT> {\n        static const bool value = false;\n    };\n}\n\n",[21,344,345,353,359,372,395,399],{"__ignoreMap":114},[118,346,347,349,351],{"class":120,"line":121},[118,348,238],{"class":135},[118,350,241],{"class":124},[118,352,244],{"class":128},[118,354,355,357],{"class":120,"line":174},[118,356,249],{"class":135},[118,358,252],{"class":128},[118,360,361,363,366,368,370],{"class":120,"line":189},[118,362,257],{"class":135},[118,364,365],{"class":124}," is_range",[118,367,263],{"class":128},[118,369,162],{"class":124},[118,371,268],{"class":128},[118,373,374,376,379,382,385,388,392],{"class":120,"line":198},[118,375,273],{"class":135},[118,377,378],{"class":135}," const",[118,380,381],{"class":135}," bool",[118,383,384],{"class":128}," value ",[118,386,387],{"class":135},"=",[118,389,391],{"class":390},"sDLfK"," false",[118,393,394],{"class":128},";\n",[118,396,397],{"class":120,"line":298},[118,398,317],{"class":128},[118,400,401],{"class":120,"line":308},[118,402,201],{"class":128},[102,404,83],{"id":405},"exceptions",[13,407,408,409,412,413,416,417,419,420,422,423,426],{},"By default all exceptions deriving from ",[21,410,411],{},"std::exception"," will be translated to strings by calling the ",[21,414,415],{},"what()"," method. For exception types that do not derive from ",[21,418,411],{}," - or if ",[21,421,415],{}," does not return a suitable string - use ",[21,424,425],{},"CATCH_TRANSLATE_EXCEPTION",". This defines a function that takes your exception type, by reference, and returns a string. It can appear anywhere in the code - it doesn't have to be in the same translation unit. For example:",[109,428,430],{"className":111,"code":429,"language":113,"meta":114,"style":114},"CATCH_TRANSLATE_EXCEPTION( MyType const& ex ) {\n    return ex.message();\n}\n",[21,431,432,445,458],{"__ignoreMap":114},[118,433,434,436,439,442],{"class":120,"line":121},[118,435,425],{"class":124},[118,437,438],{"class":128},"( MyType ",[118,440,441],{"class":135},"const&",[118,443,444],{"class":128}," ex ) {\n",[118,446,447,449,452,455],{"class":120,"line":174},[118,448,192],{"class":135},[118,450,451],{"class":128}," ex.",[118,453,454],{"class":124},"message",[118,456,457],{"class":128},"();\n",[118,459,460],{"class":120,"line":189},[118,461,201],{"class":128},[102,463,89],{"id":464},"enums",[10,466,467],{},[13,468,469],{},"Introduced in Catch2 2.8.0.",[13,471,472,473,475,476,479,480,483,484,487,488,490],{},"Enums that already have a ",[21,474,180],{}," overload for ",[21,477,478],{},"std::ostream"," will convert to strings as expected.\nIf you only need to convert enums to strings for test reporting purposes you can provide a ",[21,481,482],{},"StringMaker"," specialisations as any other type.\nHowever, as a convenience, Catch provides the ",[21,485,486],{},"CATCH_REGISTER_ENUM"," helper macro that will generate the ",[21,489,482],{}," specialisation for you with minimal code.\nSimply provide it the (qualified) enum name, followed by all the enum values, and you're done!",[13,492,493],{},"E.g.",[109,495,497],{"className":111,"code":496,"language":113,"meta":114,"style":114},"enum class Fruits { Banana, Apple, Mango };\n\nCATCH_REGISTER_ENUM( Fruits, Fruits::Banana, Fruits::Apple, Fruits::Mango )\n\nTEST_CASE() {\n    REQUIRE( Fruits::Mango == Fruits::Apple );\n}\n",[21,498,499,529,535,558,562,569,589],{"__ignoreMap":114},[118,500,501,504,507,510,513,516,518,521,523,526],{"class":120,"line":121},[118,502,503],{"class":135},"enum",[118,505,506],{"class":135}," class",[118,508,509],{"class":124}," Fruits",[118,511,512],{"class":128}," { ",[118,514,515],{"class":390},"Banana",[118,517,36],{"class":128},[118,519,520],{"class":390},"Apple",[118,522,36],{"class":128},[118,524,525],{"class":390},"Mango",[118,527,528],{"class":128}," };\n",[118,530,531],{"class":120,"line":174},[118,532,534],{"emptyLinePlaceholder":533},true,"\n",[118,536,537,539,542,545,548,550,553,555],{"class":120,"line":189},[118,538,486],{"class":124},[118,540,541],{"class":128},"( Fruits, ",[118,543,544],{"class":124},"Fruits",[118,546,547],{"class":128},"::Banana, ",[118,549,544],{"class":124},[118,551,552],{"class":128},"::Apple, ",[118,554,544],{"class":124},[118,556,557],{"class":128},"::Mango )\n",[118,559,560],{"class":120,"line":198},[118,561,534],{"emptyLinePlaceholder":533},[118,563,564,566],{"class":120,"line":298},[118,565,23],{"class":124},[118,567,568],{"class":128},"() {\n",[118,570,571,574,576,578,581,584,586],{"class":120,"line":308},[118,572,573],{"class":124},"    REQUIRE",[118,575,287],{"class":128},[118,577,544],{"class":124},[118,579,580],{"class":128},"::Mango ",[118,582,583],{"class":135},"==",[118,585,509],{"class":124},[118,587,588],{"class":128},"::Apple );\n",[118,590,591],{"class":120,"line":314},[118,592,201],{"class":128},[13,594,595],{},"... or if the enum is in a namespace:",[109,597,599],{"className":111,"code":598,"language":113,"meta":114,"style":114},"namespace Bikeshed {\n    enum class Colours { Red, Green, Blue };\n}\n\n\u002F\u002F Important!: This macro must appear at top level scope - not inside a namespace\n\u002F\u002F You can fully qualify the names, or use a using if you prefer\nCATCH_REGISTER_ENUM( Bikeshed::Colours,\n    Bikeshed::Colours::Red,\n    Bikeshed::Colours::Green,\n    Bikeshed::Colours::Blue )\n\nTEST_CASE() {\n    REQUIRE( Bikeshed::Colours::Red == Bikeshed::Colours::Blue );\n}\n",[21,600,601,610,637,641,645,651,656,668,681,693,705,710,717,744],{"__ignoreMap":114},[118,602,603,605,608],{"class":120,"line":121},[118,604,238],{"class":135},[118,606,607],{"class":124}," Bikeshed",[118,609,244],{"class":128},[118,611,612,615,617,620,622,625,627,630,632,635],{"class":120,"line":174},[118,613,614],{"class":135},"    enum",[118,616,506],{"class":135},[118,618,619],{"class":124}," Colours",[118,621,512],{"class":128},[118,623,624],{"class":390},"Red",[118,626,36],{"class":128},[118,628,629],{"class":390},"Green",[118,631,36],{"class":128},[118,633,634],{"class":390},"Blue",[118,636,528],{"class":128},[118,638,639],{"class":120,"line":189},[118,640,201],{"class":128},[118,642,643],{"class":120,"line":198},[118,644,534],{"emptyLinePlaceholder":533},[118,646,647],{"class":120,"line":298},[118,648,650],{"class":649},"sAwPA","\u002F\u002F Important!: This macro must appear at top level scope - not inside a namespace\n",[118,652,653],{"class":120,"line":308},[118,654,655],{"class":649},"\u002F\u002F You can fully qualify the names, or use a using if you prefer\n",[118,657,658,660,662,665],{"class":120,"line":314},[118,659,486],{"class":124},[118,661,287],{"class":128},[118,663,664],{"class":124},"Bikeshed",[118,666,667],{"class":128},"::Colours,\n",[118,669,670,673,675,678],{"class":120,"line":320},[118,671,672],{"class":124},"    Bikeshed",[118,674,129],{"class":128},[118,676,677],{"class":124},"Colours",[118,679,680],{"class":128},"::Red,\n",[118,682,684,686,688,690],{"class":120,"line":683},9,[118,685,672],{"class":124},[118,687,129],{"class":128},[118,689,677],{"class":124},[118,691,692],{"class":128},"::Green,\n",[118,694,696,698,700,702],{"class":120,"line":695},10,[118,697,672],{"class":124},[118,699,129],{"class":128},[118,701,677],{"class":124},[118,703,704],{"class":128},"::Blue )\n",[118,706,708],{"class":120,"line":707},11,[118,709,534],{"emptyLinePlaceholder":533},[118,711,713,715],{"class":120,"line":712},12,[118,714,23],{"class":124},[118,716,568],{"class":128},[118,718,720,722,724,726,728,730,733,735,737,739,741],{"class":120,"line":719},13,[118,721,573],{"class":124},[118,723,287],{"class":128},[118,725,664],{"class":124},[118,727,129],{"class":128},[118,729,677],{"class":124},[118,731,732],{"class":128},"::Red ",[118,734,583],{"class":135},[118,736,607],{"class":124},[118,738,129],{"class":128},[118,740,677],{"class":124},[118,742,743],{"class":128},"::Blue );\n",[118,745,747],{"class":120,"line":746},14,[118,748,201],{"class":128},[102,750,95],{"id":751},"floating-point-precision",[10,753,754],{},[13,755,756,762],{},[45,757,761],{"href":758,"rel":759},"https:\u002F\u002Fgithub.com\u002Fcatchorg\u002FCatch2\u002Fissues\u002F1614",[760],"nofollow","Introduced"," in Catch2 2.8.0.",[13,764,765,766,768,769,772,773,776,777,780,781,783],{},"Catch provides a built-in ",[21,767,482],{}," specialization for both ",[21,770,771],{},"float","\nand ",[21,774,775],{},"double",". By default, it uses what we think is a reasonable precision,\nbut you can customize it by modifying the ",[21,778,779],{},"precision"," static variable\ninside the ",[21,782,482],{}," specialization, like so:",[109,785,787],{"className":111,"code":786,"language":113,"meta":114,"style":114},"        Catch::StringMaker\u003Cfloat>::precision = 15;\n        const float testFloat1 = 1.12345678901234567899f;\n        const float testFloat2 = 1.12345678991234567899f;\n        REQUIRE(testFloat1 == testFloat2);\n",[21,788,789,812,833,851],{"__ignoreMap":114},[118,790,791,794,796,798,800,802,805,807,810],{"class":120,"line":121},[118,792,793],{"class":124},"        Catch",[118,795,129],{"class":128},[118,797,482],{"class":124},[118,799,263],{"class":128},[118,801,771],{"class":135},[118,803,804],{"class":128},">::precision ",[118,806,387],{"class":135},[118,808,809],{"class":390}," 15",[118,811,394],{"class":128},[118,813,814,817,820,823,825,828,831],{"class":120,"line":174},[118,815,816],{"class":135},"        const",[118,818,819],{"class":135}," float",[118,821,822],{"class":128}," testFloat1 ",[118,824,387],{"class":135},[118,826,827],{"class":390}," 1.12345678901234567899",[118,829,830],{"class":135},"f",[118,832,394],{"class":128},[118,834,835,837,839,842,844,847,849],{"class":120,"line":189},[118,836,816],{"class":135},[118,838,819],{"class":135},[118,840,841],{"class":128}," testFloat2 ",[118,843,387],{"class":135},[118,845,846],{"class":390}," 1.12345678991234567899",[118,848,830],{"class":135},[118,850,394],{"class":128},[118,852,853,856,859,861],{"class":120,"line":198},[118,854,855],{"class":124},"        REQUIRE",[118,857,858],{"class":128},"(testFloat1 ",[118,860,583],{"class":135},[118,862,863],{"class":128}," testFloat2);\n",[13,865,866,867,24,870,873],{},"This assertion will fail and print out the ",[21,868,869],{},"testFloat1",[21,871,872],{},"testFloat2","\nto 15 decimal places.",[875,876,877],"style",{},"html pre.shiki code .svObZ, html code.shiki .svObZ{--shiki-default:#B392F0}html pre.shiki code .s95oV, html code.shiki .s95oV{--shiki-default:#E1E4E8}html pre.shiki code .snl16, html code.shiki .snl16{--shiki-default:#F97583}html pre.shiki code .s9osk, html code.shiki .s9osk{--shiki-default:#FFAB70}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html pre.shiki code .sDLfK, html code.shiki .sDLfK{--shiki-default:#79B8FF}html pre.shiki code .sAwPA, html code.shiki .sAwPA{--shiki-default:#6A737D}",{"title":114,"searchDepth":174,"depth":174,"links":879},[880,881,882,883,884,885],{"id":104,"depth":174,"text":65},{"id":217,"depth":174,"text":71},{"id":325,"depth":174,"text":77},{"id":405,"depth":174,"text":83},{"id":464,"depth":174,"text":89},{"id":751,"depth":174,"text":95},"md",{},"\u002Fdocs\u002Ftostring",{"title":5,"description":114},"tostring","docs\u002Ftostring.md","docs\u002Ftostring","ciT-mfRTYFepgYHweOU-SO8KdFTGY_iqGuKYedZH9Hw",1794961594730]