接続数

COUNTER369444FROM 2012/5/16

MISRA-C diary(C言語日誌)

MISRA-C >> 記事詳細

2014/06/18

cpp2011//20.2.3 forward/move helpers

Tweet ThisSend to Facebook | by kaizen
// 1 filename:cpp2011-20-2-3.cpp
// ver 0.1 June.12, 2014
//
// 2 original examples and/or notes:
// (c) ISO/IEC JTC1 SC22 WG21 N3242, April 12, 2011
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf
// >20 General utilities library 20.2 Utility components 20.2.3 forward/move helpers
//
// 3 compile and output mechanism:
// (c) Dr. OGAWA Kiyoshi, kaizen at gifu-u.ac.jp,
//
// 4 compile errors and/or warnings:
// 4.1(c) Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
// Target: x86_64-apple-darwin13.2.0,  Thread model: posix
// Command/Options: c++ -std=c++11 -stdlib=libc++ -Wall cpp2011-20-2-3.cpp  
// (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign.

// 4.2. g++-4.9 (GCC) 4.9.0 20131229 (experimental)
// Copyright (C) 2013 Free Software Foundation, Inc.
// This is free software; see the source for copying conditions.  
// There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// http://gcc.gnu.org/onlinedocs/gcc/Standards.html
// Command/Options: g++-4.9  -std=c++11  -Wall cpp2011-20-2-3.cpp 
// g++-4.9: error: unrecognized command line option '-stdlib=libc++'
// Configuration:brew install gcc49
//
// 4.3. Visual Studio Express 2013, 
// (c) Microsoft http://www.visualstudio.com/
// SPEC:
// Windows 7, .NET Framework
// (c) VMware, Inc.
// VMWare fusion 6
//
// 5. Hardware:  MacBook Pro, 
//(c) Intel http://ark.intel.com/products/37006/
//Core 2 Duo 2.53GHz, 8GB, 1067MHz DDR3
//
// 6. Special Thanks: Upper organizatios and 
// ITSCJ/IPSJ http://www.itscj.ipsj.or.jp/itscj_english/index.html
// Renesas Electronics Corporation.http://www.renesas.com/
// NPO SESSAME project, http://www.sessame.jp/workinggroup/WorkingGroup3/
// Toyo Corporation, http://www.toyo.co.jp/English/
// Japan Standard Association, http://bit.ly/1lzykg1
// NPO TOPPERS project, https://www.toppers.jp/asp-d-download.html
// Daido Universcity, http://www.daido-it.ac.jp/gakubugakka/computer/index.html
// WITZ Co.Ltd., http://www.witz-inc.co.jp/products/solution/solution.html
// SevenWise.co., http://www.7ws.co.jp/index.html
// TOYOTA Motor Corporation, http://toyota.jp/
// IT planning Inc., http://www.itpl.co.jp/en/index.html
// DENSO Corporation, http://www.globaldenso.com/en/
// Aisin Seiki co. Ltd., http://www.aisin.com/
// Spancion Inc., http://www.spansion.com/
// Yazaki Corporation, http://www.yazaki-group.com/global/
// Pananosic Corporation, http://www.panasonic.net/
// SWEST: Summer Workshop on Embedded System Technologies , http://swest.toppers.jp
// CEST: Consortium for Embedded System Technology, http://www.ertl.jp/CEST/
// JUSE: Union of Japanese Scientists and Engineers, http://www.juse.or.jp/e/
// OSC:Open Source Conference, http://www.ospn.jp/
//
//#define __LIBCPP_TYPEINFO
// typeinfo synopsis
//
#include <iostream>
namespace std {
class logic_error;
class domain_error;
class invalid_argument;
class length_error;
class out_of_range;
class runtime_error;
class range_error;
class overflow_error;
class underflow_error;
}
//#include <cstdlib>
//#include <string.h>
//#include <cstring>
//#include <type_traits>
//#include <cassert>
//#include <array>
//#include <algorithm>
//#include <cmath>
//#include <complex>
//#include <ccomplex>
//#include <assert.h>
//#include <utility>
//#include <iosfwd>
//#include <iterator>
//#include <limits>
//#include <cstdint>
//#include <typeinfo>
//#include <exception>
#include <initializer_list>

namespace std {
// 20.2.1, operators:
namespace rel_ops {
template<class T> bool operator!=(const T&, const T&);
template<class T> bool operator> (const T&, const T&);
template<class T> bool operator<=(const T&, const T&);
template<class T> bool operator>=(const T&, const T&);
}
// 20.2.2, swap:
template<class T> void swap(T& a, T& b) noexcept;
//template<class T> typename conditional<is_nothrow_move_constructible<T>::value && is_nothrow_move_assignable<T>::value, T& a, T& b>::type swap(T&a, T&b) noexcept;
template <class T, size_t N> void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)));

// 20.2.3, forward/move:
template <class T> T&& forward(typename remove_reference<T>::type& t) noexcept;
template <class T> T&& forward(typename remove_reference<T>::type&& t) noexcept;
template <class T> typename remove_reference<T>::type&& move(T&&) noexcept;
template <class T> typename conditional<!is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value, const T&, T&&>::type move_if_noexcept(T& x) noexcept;
template <class T> T&& forward(typename remove_reference<T>::type& t) noexcept;
template <class T> T&& forward(typename remove_reference<T>::type&& t) noexcept;
//
template <class T, class A1, class A2>
shared_ptr<T> factory(A1&& a1, A2&& a2) {
return shared_ptr<T>(new T(std::forward<A1>(a1), std::forward<A2>(a2)));
}
struct A {
A(int&, const double&);
};
void g() {
//error: shared_ptr<A> sp1 = factory<A>(2, 1.414); // error: 2 will not bind to int&
int i = 2;
shared_ptr<A> sp2 = factory<A>(i, 1.414); // OK
}
//
template <class T> typename remove_reference<T>::type&& move(T&& t) noexcept;
//
template <class T, class A1>
shared_ptr<T> factory(A1&& a1) {
return shared_ptr<T>(new T(std::forward<A1>(a1)));
}
struct A2 {
A2();
A2(const A2&); // copies from lvalues
A2(A2&&); // moves from rvalues
};
void g2() {
A2 a;
shared_ptr<A2> sp1 = factory<A2>(a); // “a” binds to A(const A&)
//error: shared_ptr<A2> sp1 = factory<A2>(std::move(a)); // “a” binds to A(A&&)
}
//
template <class T> typename conditional<
!is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value,
const T&, T&&>::type move_if_noexcept(T& x) noexcept;
//

// 20.2.4, declval:
template <class T> typename add_rvalue_reference<T>::type declval() noexcept; // as unevaluated operand
// 20.3, pairs:
template <class T1, class T2> struct pair;
// 20.3.3, pair specialized algorithms:
template <class T1, class T2> bool operator==(const pair<T1,T2>&, const pair<T1,T2>&);
template <class T1, class T2> bool operator< (const pair<T1,T2>&, const pair<T1,T2>&);
template <class T1, class T2> bool operator!=(const pair<T1,T2>&, const pair<T1,T2>&);
template <class T1, class T2> bool operator> (const pair<T1,T2>&, const pair<T1,T2>&);
template <class T1, class T2> bool operator>=(const pair<T1,T2>&, const pair<T1,T2>&);
template <class T1, class T2> bool operator<=(const pair<T1,T2>&, const pair<T1,T2>&);
template <class T1, class T2> void swap(pair<T1,T2>& x, pair<T1,T2>& y) noexcept(noexcept(x.swap(y)));
//error: template <class T1, class T2> see below make_pair(T1&&, T2&&); //  error: unknown type name 'see', change like below
//error: template <class T1, class T2> pair<V1, V2> make_pair(T1&& x, T2&& y) noexcept;
//
// 20.3.4, tuple-like access to pair:
template <class T> class tuple_size;
template <size_t I, class T> class tuple_element;
template <class T1, class T2> struct tuple_size<std::pair<T1, T2> >;
template <class T1, class T2> struct tuple_element<0, std::pair<T1, T2> >;
template <class T1, class T2> struct tuple_element<1, std::pair<T1, T2> >;
template<size_t I, class T1, class T2> typename tuple_element<I, std::pair<T1, T2> >::type& get(std::pair<T1, T2>&) noexcept;
template<size_t I, class T1, class T2> typename tuple_element<I, std::pair<T1, T2> >::type&& get(std::pair<T1, T2>&&) noexcept;
template<size_t I, class T1, class T2> const typename tuple_element<I, std::pair<T1, T2> >::type& get(const std::pair<T1, T2>&) noexcept;
// 20.3.5, pair piecewise construction
struct piecewise_construct_t { };
constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
template <class... Types> class tuple; // defined in <tuple>
}

using namespace std;

template<class T> void swap(T& a, T& b) noexcept;
//

//
template<class T, size_t N> void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)));
//
int main() {
//
cout << "20 General utilities library 20.2 Utility components 20.2.3 forward/move helpers"<<std::endl;
return 0;
}
// 1. error
// 1.1 llvm: c++ -std=c++11 -stdlib=libc++ -Wall cpp2011-20-2-3.cpp
cpp2011-20-2-3.cpp:169:31: warning: struct template 'tuple_size' was previously declared as a class template [-Wmismatched-tags]
template <class T1, class T2> struct tuple_size<std::pair<T1, T2> >;
                              ^
cpp2011-20-2-3.cpp:167:26: note: previous use is here
template <class T> class tuple_size;
                         ^
cpp2011-20-2-3.cpp:170:31: warning: struct template 'tuple_element' was previously declared as a class template [-Wmismatched-tags]
template <class T1, class T2> struct tuple_element<0, std::pair<T1, T2> >;
                              ^
cpp2011-20-2-3.cpp:168:36: note: previous use is here
template <size_t I, class T> class tuple_element;
                                   ^
cpp2011-20-2-3.cpp:171:31: warning: struct template 'tuple_element' was previously declared as a class template [-Wmismatched-tags]
template <class T1, class T2> struct tuple_element<1, std::pair<T1, T2> >;
                              ^
cpp2011-20-2-3.cpp:168:36: note: previous use is here
template <size_t I, class T> class tuple_element;
                                   ^
cpp2011-20-2-3.cpp:118:28: error: call to 'forward' is ambiguous
return shared_ptr<T>(new T(std::forward<A1>(a1), std::forward<A2>(a2)));
                           ^~~~~~~~~~~~~~~~
cpp2011-20-2-3.cpp:126:21: note: in instantiation of function template specialization 'std::factory<std::A, int &, double>' requested here
shared_ptr<A> sp2 = factory<A>(i, 1.414); // OK
                    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/type_traits:1518:1: note: candidate function
      [with _Tp = int &]
forward(typename std::remove_reference<_Tp>::type& __t) _NOEXCEPT
^
cpp2011-20-2-3.cpp:113:24: note: candidate function [with T = int &]
template <class T> T&& forward(typename remove_reference<T>::type& t) noexcept;
                       ^
cpp2011-20-2-3.cpp:133:28: error: call to 'forward' is ambiguous
return shared_ptr<T>(new T(std::forward<A1>(a1)));
                           ^~~~~~~~~~~~~~~~
cpp2011-20-2-3.cpp:142:22: note: in instantiation of function template specialization 'std::factory<std::A2, std::A2 &>' requested here
shared_ptr<A2> sp1 = factory<A2>(a); // “a” binds to A(const A&)
                     ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/type_traits:1518:1: note: candidate function
      [with _Tp = std::A2 &]
forward(typename std::remove_reference<_Tp>::type& __t) _NOEXCEPT
^
cpp2011-20-2-3.cpp:113:24: note: candidate function [with T = std::A2 &]
template <class T> T&& forward(typename remove_reference<T>::type& t) noexcept;
                       ^
3 warnings and 2 errors generated.


10:19 | 投票する | 投票数(0) | コメント(0)