Skip to content

Catch is susceptible to VC12 bug: do-while loop within range-based-for fails to compile. #315

Closed
@martinmoene

Description

Compiling:

#define CATCH_CONFIG_MAIN
#include "catch.hpp"

TEST_CASE( "Syntax error with VC12", "" )
{
    for ( auto x : { 1 , 2, 3 } )
        REQUIRE( x < 3.14 );
}

yields:

range-for-catch.cpp(7) : error C2059: syntax error : '}'

This VC12 behaviour is reported as do-while loop within range-based-for fails to compile.

See also Range-for, with try/catch statement, not compiling.

Workaround for users of Catch:

TEST_CASE( "Enclose in block to compile with VC12", "" )
{
    for ( auto x : { 1 , 2, 3 } )
    {
        REQUIRE( x < 3.14 );
    }
}

With Catch REQUIRE expanded and simplified this boils down to:

void fail()
{
    for ( auto x: { 1, 2, 3 } )
        do
        {
            try { }
            catch( std::exception const & ) {} 
        }
        while ( false );
}

Workaround is to enclose do {} while(); in block:

void pass()
{
    for ( auto x: { 1, 2, 3 } )
    {
        do
        {
            try { }
            catch( std::exception const & ) {} 
        }
        while ( false );
    }
}

This changes REQUIRE() from an iteration statement into a compound statement. What consequences may this have, if any?

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions