473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

solution for max_length_string (?)

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)

{
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;
}
}


Jul 22 '05 #1
9 1698
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;

};

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
Jul 22 '05 #2
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;

};

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

Jul 22 '05 #3
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;

};

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
Jul 22 '05 #4
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;

};

template< int M >
friend typename max_lenght_string< N >::X< M >::result_type;
// friend typename max_lenght_string::X< M >::result_type;
// max_lenght_string< M > should be friend if N <= M
// if N > M null_type will be friend

public:
...
template< int M >
max_lenght_string( max_lenght_string< M > const& a)
:
str_( a.str_) // works only if max_lenght_string< N > is friend
// of max_lenght_string< M >
{}
....
but ms vc 7.1 will not compile for the friend declaration!
any suggestions?
thx,
Oliver

Jul 22 '05 #5
Oliver Kowalke wrote:
....
// max_lenght_string< 1 > a7("abc"); // run-time error


I think you can make this a compile time error.

Jul 22 '05 #6
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 (no static access; only friend template declaration):

....
private:

template< int M >
struct X
{
typedef typename if_then_else< N <= M, max_lenght_string< M >, null_type
::result_type result_type;

};

template< int M >
friend typename max_lenght_string< N >::X< M >::result_type;
// friend typename max_lenght_string::X< M >::result_type;
// max_lenght_string< M > should be friend if N <= M
// if N > M null_type will be friend

public:
...
template< int M >
max_lenght_string( max_lenght_string< M > const& a)
:
str_( a.str_) // works only if max_lenght_string< N > is friend
// of max_lenght_string< M >
{}
....
but ms vc 7.1 will not compile for the friend declaration!
any suggestions?
thx,
Oliver

Jul 22 '05 #7
Gianni Mariani wrote:
Oliver Kowalke wrote:
...
// max_lenght_string< 1 > a7("abc"); // run-time error


I think you can make this a compile time error.


i'm not sure but i think on compile-time i didn't know what size the string
will have.
maybe you are so kind to tell me your solution?

Jul 22 '05 #8
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 (no static access; only friend template declaration):

....
private:

template< int M >
struct X
{
typedef typename if_then_else< N <= M, max_lenght_string< M >, null_type
::result_type result_type;

};

template< int M >
friend typename max_lenght_string< N >::X< M >::result_type;
// friend typename max_lenght_string::X< M >::result_type;
// max_lenght_string< M > should be friend if N <= M
// if N > M null_type will be friend

public:
...
template< int M >
max_lenght_string( max_lenght_string< M > const& a)
:
str_( a.str_) // works only if max_lenght_string< N > is friend
// of max_lenght_string< M >
{}
....
but ms vc 7.1 will not compile for the friend declaration!
any suggestions?
thx,
Oliver

Jul 22 '05 #9
Oliver Kowalke wrote:
Gianni Mariani wrote:

Oliver Kowalke wrote:
...
// max_lenght_string< 1 > a7("abc"); // run-time error


I think you can make this a compile time error.

i'm not sure but i think on compile-time i didn't know what size the string
will have.
maybe you are so kind to tell me your solution?


You can get access to the size using a template but you need to change
the max_lenght_string( const char * c) constructor to be a template
also, otherwise it won't happen.
template <typename T>
max_lenght_string( T * c)
: str_( max_lenght_string::check( std::string( c) ) )
{}

template <unsigned SL>
max_lenght_string( char const (& str)[SL] )
: str_( max_lenght_string::str_access<SL>( str ) )
{}
BTW - I had a hard time compiling your example on gcc.

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Wayne Wengert | last post by:
I have a solution. I want to clone it to try some different options. Is there a way to open Solution A, save it as Solution B and then be able to try some things in Solution B without affecting...
10
by: Simon Jefferies | last post by:
Hello, I'm getting a strange problem where when I load up my solution that has three projects:- a managed C++ .NET, one C++ lib, and a VB library. It locks up and doesn't load completely, I...
0
by: TEK | last post by:
Hello We have a quite huge project. To limit the solution size, rebuild time and so on we have divided the project in two different solution. One solution that holds the buiness entities, or...
6
by: I_AM_DON_AND_YOU? | last post by:
I have a small VB.Net Solution. The solution has filename LABELMAKER.SLN and the the project has filename LABELMAKER.VBPROJ. There is another file LABELMAKER.VBPROJ.USER. All these are in a...
5
by: kai | last post by:
Hi, I use XP Pro and VS2005. I find when I create a new Website, I only see the Project node not Solution. After I add sedcond project under the solution tree, then I see the solution node. How...
2
by: mkd1919 | last post by:
I'm developing a web service that will eventually be sent to other like organizations to communicate for a particular application. After a few weeks, I decided it would be better to split of the...
3
by: Manikandan | last post by:
Hi, I'm copying projects from a solution in the vss. In my local system i created solution ,added that project(make modification related to solution) When i added this solution to vss, projects...
0
by: techie | last post by:
List of Solution manuals: Engineering Circuit Analysis 6Ed - Hayt Solutions Manual Norbury - Solutions manual for mechanics and thermodynamics Physics For Scientists And Engineers - Solution...
16
by: =?Utf-8?B?RHdlZWJlcmVsbGE=?= | last post by:
I created an Access 2007 application for my customer. The application is shared by three employees on a server. It maintains a contact list including financial data and social security numbers. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.