質問

Boost 1.55、MSVC Express 2012。 トリビールによる誤った式評価 Tribool(False)を明示的に指定した場合にのみ機能します。

物語の道徳:コンパイラは値に基づいてタイプを選択します。

auto a = 0? indeterminate : false; // type function pointer
auto b = 0? indeterminate : true; // type bool
.

出力:

  1. インデット:1?不定:偽
  2. インデット:0?不定:偽
  3. true:1?不定:true
  4. true:0?不定:true
  5. インデット:1?不定:トリビール(偽)
  6. false:0?不定:トリビール(偽)
  7. インデット:1?不定:トリビール(真)
  8. true:0?不定:トリビール(真)
  9. ソースコード:

    #include <iostream>
    #include <boost/logic/tribool.hpp>
    
    using namespace boost;
    
    void test_tribool( const tribool& v, const char* name )
    {
        const char* s;
        if ( v )
            s = "true";
        else if ( !v )
            s = "false";
        else
            s = "indet";
        std::cout << s << "\t: " << name << std::endl;
    }
    
    #define TEST_TRIBOOL( ... ) test_tribool( (__VA_ARGS__), #__VA_ARGS__ );
    
    int main(int argc, char** argv)
    {
        TEST_TRIBOOL( 1? indeterminate : false );
        TEST_TRIBOOL( 0? indeterminate : false );
    
        // warning C4305: ':' : truncation from 'bool (__cdecl *)(boost::logic::tribool,boost::logic::detail::indeterminate_t)' to 'bool'
        TEST_TRIBOOL( 1? indeterminate : true );
    
        // warning C4305: ':' : truncation from 'bool (__cdecl *)(boost::logic::tribool,boost::logic::detail::indeterminate_t)' to 'bool'
        TEST_TRIBOOL( 0? indeterminate : true );
    
    
        TEST_TRIBOOL( 1? indeterminate : tribool(false) );
        TEST_TRIBOOL( 0? indeterminate : tribool(false) );
        TEST_TRIBOOL( 1? indeterminate : tribool(true) );
        TEST_TRIBOOL( 0? indeterminate : tribool(true) );
    
        return 0;
    }
    
    .

役に立ちましたか?

解決

これらは異なるタイプで、MSVCはこれについての警告を正当に与えているはずです。自分のhref="http://msdn.microsoft.com/jp/jaus-us/library/e4213hs1.aspx" rel="nofollow"> Documentation :

 The following rules apply to the second and third expressions:

     If both expressions are of the same type, the result is of that type.

     If both expressions are of arithmetic or enumeration types, 
     the usual arithmetic conversions (covered in Arithmetic Conversions)
     are performed to convert them to a common type.

     If both expressions are of pointer types or if one is a pointer type 
     and the other is a constant expression that evaluates to 0,
     pointer conversions are performed to convert them to a common type.

     If both expressions are of reference types, reference conversions 
     are performed to convert them to a common type.

     If both expressions are of type void, the common type is type void.

     If both expressions are of a given class type, the common type is 
     that class type.

 Any combinations of second and third operands not in the preceding
 list are illegal. The type of the result is the common type, and it is
 an l-value if both the second and third operands are of the same type
 and both are l-values.
.

あなたの三国演算子は同じタイプを返さないので、ブールと不確定の組み合わせのために、結果はおそらく

と一致する変換を受けます
     If both expressions are of pointer types or if one is a pointer type 
     and the other is a constant expression that evaluates to 0,
     pointer conversions are performed to convert them to a common type.
.

と一致する

typedef bool (*indeterminate_keyword_t)(tribool, detail::indeterminate_t);
. tribool.hpp false値の代わりに「評価」されている機能ポインタです。

だから、?オペレータが同じタイプを返す必要があります。マクロを変更するように変更します。

TEST_TRIBOOL( 1 ? tribool(indeterminate) : tribool(false));
.

またはあるいは

const tribool t_indet(indeterminate);
const tribool t_false(false);
const tribool t_true(true);

TEST_TRIBOOL( 1 ? t_indet : t_false );
TEST_TRIBOOL( 0 ? t_indet : t_false );
...
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top