⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 another_tuple_test_bench.cpp

📁 C++的一个好库。。。现在很流行
💻 CPP
字号:
// Copyright (C) 1999, 2000 Jaakko J鋜vi (jaakko.jarvi@cs.utu.fi)
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

// For more information, see http://www.boost.org


//  another_test_bench.cpp  --------------------------------

// This file has various tests to see that things that shouldn't
// compile, don't compile.

// Defining any of E1 to E5 or E7 to E11 opens some illegal code that 
// should cause the compliation to fail.

#define BOOST_INCLUDE_MAIN  // for testing, include rather than link
#include <boost/test/test_tools.hpp>    // see "Header Implementation Option"

#include "boost/tuple/tuple.hpp"

#include <string>
#include <utility>

using namespace std;
using namespace boost;
using namespace boost::tuples;


template<class T> void dummy(const T&) {}

class A {}; class B {}; class C {};

// A non-copyable class
class no_copy {
  no_copy(const no_copy&) {}
public:
  no_copy() {};
};

no_copy y;

#ifdef E1
tuple<no_copy> v1;  // should faild
#endif


#ifdef E2
char cs[10];
tuple<char[10]> v3;  // should fail, arrays must be stored as references
#endif

// a class without a public default constructor
class no_def_constructor {
  no_def_constructor() {}
public:
  no_def_constructor(std::string) {} // can be constructed with a string
};

void foo1() {

#ifdef E3
  dummy(tuple<no_def_constructor, no_def_constructor, no_def_constructor>()); 
  // should fail

#endif
}

void foo2() {
// testing default values
#ifdef E4
  dummy(tuple<double&>()); // should fail, not defaults for references
  dummy(tuple<const double&>()); // likewise
#endif

#ifdef E5
  double dd = 5;
  dummy(tuple<double&>(dd+3.14)); // should fail, temporary to non-const reference
#endif
}



// make_tuple ------------------------------------------


   void foo3() {
#ifdef E7
    std::make_pair("Doesn't","Work"); // fails
#endif
    //    make_tuple("Does", "Work"); // this should work
}



// - testing element access

void foo4() 
{
  double d = 2.7; 
  A a;
  tuple<int, double&, const A&> t(1, d, a);
  const tuple<int, double&, const A> ct = t;
  (void)ct;
#ifdef E8
  get<0>(ct) = 5; // can't assign to const
#endif

#ifdef E9
  get<4>(t) = A(); // can't assign to const
#endif
#ifdef E10
  dummy(get<5>(ct)); // illegal index
#endif
}

// testing copy and assignment with implicit conversions between elements
// testing tie

  class AA {};
  class BB : public AA {};
  struct CC { CC() {} CC(const BB& b) {} };
  struct DD { operator CC() const { return CC(); }; };

  void foo5() {
    tuple<char, BB*, BB, DD> t;
    (void)t;
    tuple<char, char> aaa;
    tuple<int, int> bbb(aaa);
    (void)bbb;
    //    tuple<int, AA*, CC, CC> a = t;
    //    a = t;
  }


// testing tie
// testing assignment from std::pair
void foo7() {

   tuple<int, int, float> a;
#ifdef E11
   a = std::make_pair(1, 2); // should fail, tuple is of length 3, not 2
#endif

    dummy(a);
}



// --------------------------------
// ----------------------------
int test_main(int, char *[]) {

  foo1();
  foo2();
  foo3();
  foo4();
  foo5();

  foo7();

  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -