简体   繁体   English

奇怪? 行为的行为

[英]Strange? behaviour of cout

Why executing this code: 为什么执行此代码:

// DefaultAny.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <exception>

using std::cout;

template<class T>
struct NoReturnPolicy 
{
    static void calculate(T& result, const T& source)
    {
        result = source;
    }
};

template<class T>
struct ReturnPolicy 
{
    static T& calculate(T& result, const T& source)
    {
        result = source;
        return result;
    }
};


template<class T>
struct ThrowPolicy 
{
    static void check(T* ptr)
    {
        cout << "ThrowPolicy";
        struct Nullptr: public std::exception{};

        if(!ptr)
        {
            throw Nullptr("Nullptr not allowed"); 
        }
    }
};

template<class T>
struct NoThrowPolicy 
{
    static T* check(T* ptr)
    {
        cout << "NoThrowPolicy";
        if(!ptr)
        {
            return nullptr;
        }
        else
        {
            return ptr;
        }
    }
};


/*
If pointer already points at 
something no assignement is being done
*/
template<class T, class ThrowingPolicy>
struct NoAssignPolicy 
{
    static T* check(T* dest,const T*const src)
    {
        cout << "NoAssignPolicy";
        if (!ThrowPolicy::check(dest))
        {
            dest = operator new(sizeof(T));
            new (dest) T(*src);
        }

    }
};

template<class T,class ThrowingPolicy>
struct NoCheckPolicy
{
    static void check(T* p)
    {
        cout << "NoCheckPolicy";
    }
};


template<class T,class ThrowingPolicy>
struct CheckPolicy
{
    static void check(T* p)
    {
        cout << "CheckPolicy";
        ThrowingPolicy::check(p);
    }
};


template<
         class T,
         class ThrowingPolicy = NoThrowPolicy<T>,
         class CheckingPolicy = NoCheckPolicy<T,ThrowingPolicy>,  
         class AssigningPolicy = NoAssignPolicy<T,ThrowingPolicy>,
         class ReturningPolicy = NoReturnPolicy<T>
        >
struct BreadSlicer
{
    BreadSlicer()
    {
        cout << "Type: " << typeid(T).name() << '\n';
            cout << "ThrowingPolicy: " << ThrowingPolicy::check(0) << '\n'; //  
//<<<---------The second call to cout makes output on my console:  
//NoThrowPolicy:"NoSpace"ThrowingPolicy:"Space"000000
        }
    };

    //The words NoSpace and Space do not actually appear in my console ;) and they are in the opposite order.



int _tmain(int argc, _TCHAR* argv[])
{
    BreadSlicer<int> a;
    return 0;

}

See comments in first struct above main. 请参阅main以上第一个结构中的注释。

This is the result of unspecified behavior. 这是未指定行为的结果。 If you have: 如果你有:

cout << a() << b() << c() << endl;

The order of execution of a, b, and c is not defined (yes, their results are added to the cout stream in a predictable order, but execution of the functions is not in defined order). 没有定义a,b和c的执行顺序(是的,它们的结果以可预测的顺序添加到cout流中,但是函数的执行未按定义的顺序进行)。

If your question is why "NoThrowPolicy" gets output before "ThrowingPolicy", the answer is that there's no sequence point guaranteeing an ordering for the call to ThrowingPolicy::check(0) and the call to operator<<(cout, "ThrowingPolicy: ") . 如果您的问题是为什么“ NoThrowPolicy”在“ ThrowingPolicy”之前获得输出,则答案是没有序列点可以保证对ThrowingPolicy::check(0)的调用和对operator<<(cout, "ThrowingPolicy: ") C++ is allowed to call those functions in either order. 允许C ++以任何顺序调用这些函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM