max_length_string(?)的解决方案 [英] solution for max_length_string (?)
问题描述
我需要一个可以与stl一起使用的字符串,并且应该有一个固定的最大值。
长度。具有不同max_lenght的字符串应该是不同的类型。
从具有等于或小于max_lenght的字符串赋值/复制构造
应该是可能的,反之亦然。
是我的建议一个很好的解决方案?任何改进?或者有更好的
解决方案吗?
thx,
Oliver
#include< iosfwd>
#include< string>
#include< stdexcept>
struct null_type;
模板< bool B,typename T,typename E>
struct if_then_else;
template< typename T,typename E>
struct if_then_else< true,T,E>
{
typedef T result_type;
};
template< ; typename T,typename E>
struct if_then_else< false,T,E>
{
typedef E result_type;
};
template< ; int N>
class max_lenght_string
{
private:
std :: string str _;
模板< int M>
朋友类max_lenght_string;
模板< typename charT,typename traitsT>
friend std :: basic_ostream< charT,traitsT>&
operator<<(std :: basic_ostream< charT,traitsT>& os,max_lenght_string<
N> const& str );
模板< int M>
static
std :: string const&
access(typename if_then_else< N< = M,max_lenght_string< N> ;,
null_type> :: result_type const& str)
{
返回str.str_;
}
static
std :: string const&
check(std :: string const& str)
{
if(str.size()> N)
throw std :: runtime_error(" string ist too large");
返回str;
}
public:
max_lenght_string(std :: string const& str)< br $> b $ b:
str_(max_lenght_string :: check(str))
{}
max_lenght_string(char const * c )
:
str_(max_lenght_string :: check(std :: string(c)))
{}
max_lenght_string(max_lenght_string const& str)
:
str_(str.str_)
{}
模板< int M>
max_lenght_string(max_lenght_string< M> const& str)
:
str_(max_lenght_string< M> :: access< ; N>(str))
{}
max_lenght_string const&
operator =(max_lenght_string const& rhs)
{
max_lenght_string tmp(rhs);
str_.swap(tmp.str_);
return * this ;
}
模板< int M>
max_lenght_string const&
operator =(max_lenght_string< M> const& rhs)
{
max_lenght_string< N> tmp(rhs);
str_.swap(tmp.str_);
返回* this;
}
>
// c_str(),iterators,begin(),end(),... left open
};
template< typename charT,typename traitsT,int N>
std :: basic_ostream< charT,traitsT>&
operator<<(std :: basic_ostream< charT,traitsT>& os,max_lenght_string< N
Hi,
i need a string which can be used with the stl and should have a fixed max.
length. strings with different max_lenght should be different types.
assignment/copy construction from a string with equal or less max_lenght
should be possible and vice versa not.
is my suggestion a good solution? any improvements? or does a better
solution exist?
thx,
Oliver
#include <iosfwd>
#include <string>
#include <stdexcept>
struct null_type;
template< bool B , typename T , typename E >
struct if_then_else;
template< typename T , typename E >
struct if_then_else< true , T , E>
{
typedef T result_type;
};
template< typename T , typename E >
struct if_then_else< false , T , E>
{
typedef E result_type;
};
template< int N >
class max_lenght_string
{
private:
std::string str_;
template< int M >
friend class max_lenght_string;
template< typename charT , typename traitsT >
friend std::basic_ostream< charT , traitsT >&
operator<<( std::basic_ostream< charT , traitsT >& os, max_lenght_string<
N > const& str);
template< int M >
static
std::string const&
access( typename if_then_else< N <= M , max_lenght_string< N > ,
null_type >::result_type const& str)
{
return str.str_;
}
static
std::string const&
check( std::string const& str)
{
if( str.size() > N)
throw std::runtime_error("string ist too large");
return str;
}
public:
max_lenght_string( std::string const& str)
:
str_( max_lenght_string::check( str) )
{}
max_lenght_string( char const* c)
:
str_( max_lenght_string::check( std::string( c) ) )
{}
max_lenght_string( max_lenght_string const& str)
:
str_( str.str_ )
{}
template< int M >
max_lenght_string( max_lenght_string< M > const& str)
:
str_( max_lenght_string< M >::access< N >( str) )
{}
max_lenght_string const&
operator=( max_lenght_string const& rhs)
{
max_lenght_string tmp( rhs);
str_.swap( tmp.str_);
return *this;
}
template< int M >
max_lenght_string const&
operator=( max_lenght_string< M > const& rhs)
{
max_lenght_string< N > tmp( rhs);
str_.swap( tmp.str_);
return *this;
}
// c_str(), iterators, begin(), end(), ... left open
};
template< typename charT , typename traitsT , int N >
std::basic_ostream< charT , traitsT >&
operator<<( std::basic_ostream< charT , traitsT >& os, max_lenght_string< N
const& str)
const& str)
{
if(!os)return os;
os<< str.str_;
返回os;
}
int main(int argc,char * argv [])
{
尝试
{
max_lenght_string< 1> a1(a);
max_lenght_string< 1> a2(" b");
max_lenght_string< 2> a3(ab);
max_lenght_string< 2> a4(a2); // ok
max_lenght_string< 2> a5(a4); // ok
// max_lenght_string< 1> a6(a3); //编译时错误
// max_lenght_string< 1> A7(QUOT; ABC"); //运行时错误
a1 = a2; // ok
a3 = a1; // ok
// a2 = a3; //编译时错误
std :: cout<< a1 = << a1<< std :: endl;
std :: cout<< a2 = << a2<< std :: endl;
std :: cout<< a3 = << a3<< std :: endl;
std :: cout<< a4 = << a4<< std :: endl;
std :: cout<< a5 = << a5<< std :: endl;
返回0;
}
catch(std :: exception const& ex)
{
std :: cout<< 例外: << ex.what()<< std :: endl;
返回1;
}
catch(...)
{
std :: cout<< 未处理的异常 << std :: endl;
返回1;
}
}
{
if( ! os) return os;
os << str.str_;
return os;
}
int main(int argc, char *argv[])
{
try
{
max_lenght_string< 1 > a1("a");
max_lenght_string< 1 > a2("b");
max_lenght_string< 2 > a3("ab");
max_lenght_string< 2 > a4( a2); // ok
max_lenght_string< 2 > a5( a4); // ok
// max_lenght_string< 1 > a6( a3); // compile-time error
// max_lenght_string< 1 > a7("abc"); // run-time error
a1 = a2; // ok
a3 = a1; // ok
// a2 = a3; // compile-time error
std::cout << "a1 = " << a1 << std::endl;
std::cout << "a2 = " << a2 << std::endl;
std::cout << "a3 = " << a3 << std::endl;
std::cout << "a4 = " << a4 << std::endl;
std::cout << "a5 = " << a5 << std::endl;
return 0;
}
catch( std::exception const& ex)
{
std::cout << "exception : " << ex.what() << std::endl;
return 1;
}
catch(...)
{
std::cout << "unhandled exception" << std::endl;
return 1;
}
}
推荐答案
而不是:
....
私人:
模板< int M>
朋友类max_lenght_string;
模板< int M>
static
std :: string const&
access(typename if_then_else< N< = M,max_lenght_string< N> ;,
null_type> :: result_type const& str)
{
返回str.str_;
}
....
i试过:
....
private:
模板< int M>
struct X
{
typedef typename if_then_else< N< = M,max_lenght_string< N> ,null_type
instead of:
....
private:
template< int M >
friend class max_lenght_string;
template< int M >
static
std::string const&
access( typename if_then_else< N <= M , max_lenght_string< N > ,
null_type >::result_type const& str)
{
return str.str_;
}
....
i tried:
....
private:
template< int M >
struct X
{
typedef typename if_then_else< N <= M , max_lenght_string< N > , null_type
:: result_type> :: result_type;
::result_type >::result_type;
};
template< int M>
朋友类型名称A< N> :: X< M> :: result_type;
// A< M>如果N <= M,应该是朋友;如果N> M null_type将成为朋友
public:
...
template< int M>
A(A< M> const& a)
:
str_(a.str_)//仅适用如果A< N>是A&的朋友M>
{}
....
但ms vc 7.1将无法为朋友声明编译。
有什么建议吗?
thx,
Oliver
};
template< int M >
friend typename A< N >::X< M >::result_type;
// A< M > should be friend if N <= M; if N > M null_type will be friend
public:
...
template< int M >
A( A< M > const& a)
:
str_( a.str_) // works only if A< N > is friend of A< M >
{}
....
but ms vc 7.1 will not compile for the friend declaration.
any suggestions?
thx,
Oliver
代替:
>
....
私人:
模板< int M>
朋友类max_lenght_string;
模板< int M>
static
std :: string const&
access(typename if_then_else< N< = M,max_lenght_string< N> ;,
null_type> :: result_type const& str)
{
返回str.str_;
}
....
i试过:
....
private:
模板< int M>
struct X
{
typedef typename if_then_else< N< = M,max_lenght_string< N>,null_type
instead of:
....
private:
template< int M >
friend class max_lenght_string;
template< int M >
static
std::string const&
access( typename if_then_else< N <= M , max_lenght_string< N > ,
null_type >::result_type const& str)
{
return str.str_;
}
....
i tried:
....
private:
template< int M >
struct X
{
typedef typename if_then_else< N <= M, max_lenght_string< N >, null_type
:: result_type result_type;
::result_type result_type;
};
template< int M>
朋友类型名称A< N> :: X< M> :: result_type;
// A< M>如果N <= M,应该是朋友;如果N> M null_type将成为朋友
public:
...
template< int M>
A(A< M> const& a)
:
str_(a.str_)//仅适用如果A< N>是A&的朋友M>
{}
....
但ms vc 7.1将无法为朋友声明编译。
有什么建议吗?
thx,
奥利弗
};
template< int M >
friend typename A< N >::X< M >::result_type;
// A< M > should be friend if N <= M; if N > M null_type will be friend
public:
...
template< int M >
A( A< M > const& a)
:
str_( a.str_) // works only if A< N > is friend of A< M >
{}
....
but ms vc 7.1 will not compile for the friend declaration.
any suggestions?
thx,
Oliver
而不是:
....
私人:
模板< int M>
朋友类max_lenght_string;
模板< int M>
static
std :: string const&
access(typename if_then_else< N< = M,max_lenght_string< N> ;,
null_type> :: result_type const& str)
{
返回str.str_;
}
....
i试过:
....
private:
模板< int M>
struct X
{
typedef typename if_then_else< N< = M,max_lenght_string< M>,null_type
instead of:
....
private:
template< int M >
friend class max_lenght_string;
template< int M >
static
std::string const&
access( typename if_then_else< N <= M , max_lenght_string< N > ,
null_type >::result_type const& str)
{
return str.str_;
}
....
i tried:
....
private:
template< int M >
struct X
{
typedef typename if_then_else< N <= M, max_lenght_string< M >, null_type
:: result_type result_type;
::result_type result_type;
};
template< int M>
朋友类型名称A< N> :: X< M> :: result_type;
// A< M>如果N <= M,应该是朋友;如果N> M null_type将成为朋友
public:
...
template< int M>
A(A< M> const& a)
:
str_(a.str_)//仅适用如果A< N>是A&的朋友M>
{}
....
但ms vc 7.1将无法为朋友声明编译。
有什么建议吗?
thx,
奥利弗
};
template< int M >
friend typename A< N >::X< M >::result_type;
// A< M > should be friend if N <= M; if N > M null_type will be friend
public:
...
template< int M >
A( A< M > const& a)
:
str_( a.str_) // works only if A< N > is friend of A< M >
{}
....
but ms vc 7.1 will not compile for the friend declaration.
any suggestions?
thx,
Oliver
这篇关于max_length_string(?)的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!