C++ 奇怪?cout的行为

C++ 奇怪?cout的行为,c++,iostream,C++,Iostream,为什么要执行此代码: // 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 calcu

为什么要执行此代码:

// 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;

}
//DefaultAny.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括
#包括
使用std::cout;
模板
结构NoReturnPolicy
{
静态空隙计算(T和结果、常数T和来源)
{
结果=来源;
}
};
模板
结构返回策略
{
静态T和计算(T和结果、常数T和源)
{
结果=来源;
返回结果;
}
};
模板
结构ThrowPolicy
{
静态无效检查(T*ptr)
{

cout如果您的问题是为什么“NoThrowPolicy”在“ThrowingPolicy”之前得到输出,那么答案是没有序列点保证调用
ThrowingPolicy::check(0)
和调用
操作符的顺序,这是未指定行为的结果。如果您有:

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

cout行为不是未定义的。行为是未指定的。有很大的区别。