A
Alex Buell
I just wrote the following and have a question: Why does my code gets
it wrong with the class Simple? See last show_size<Simple> function call
in main () as below:
#include <iostream>
#include <string>
#include <limits>
class Simple
{
public:
Simple();
~Simple();
void set(std::string text);
std::string get(void);
friend std:stream& operator<<(std:stream& ostream, Simple&
simple);
private:
std::string t;
};
Simple::Simple() :
t("")
{
}
Simple::~Simple()
{
}
void Simple::set(std::string text)
{
t = text;
}
std::string Simple::get(void)
{
return t;
}
template <typename T>
const int get_n_bits()
{
return std::numeric_limits<T>::digits + 1;
}
template <typename T>
const char* pluralise(const char* singluar, const char* plural)
{
return (sizeof(T) == 1) ? singluar : plural;
}
template <typename T>
std:stream& show_size(std:stream& stream, const char* type)
{
stream << type << " has ";
stream << get_n_bits<T>() << " bits";
stream << ", has length of ";
stream << sizeof(T);
stream << " ";
stream << pluralise<T>("byte", "bytes");
stream << ".\n";
return stream;
}
int main(int argc, char *argv[])
{
show_size<bool>(std::cout, "bool");
show_size<char>(std::cout, "char");
show_size<short int>(std::cout, "short int");
show_size<int>(std::cout, "int");
show_size<long int>(std::cout, "long int");
show_size<long long>(std::cout, "long long");
show_size<float>(std::cout, "float");
show_size<double>(std::cout, "double");
show_size<long double>(std::cout, "long double");
show_size<wchar_t>(std::cout, "wchar_t");
show_size<Simple>(std::cout, "Simple");
return 0;
}
it wrong with the class Simple? See last show_size<Simple> function call
in main () as below:
#include <iostream>
#include <string>
#include <limits>
class Simple
{
public:
Simple();
~Simple();
void set(std::string text);
std::string get(void);
friend std:stream& operator<<(std:stream& ostream, Simple&
simple);
private:
std::string t;
};
Simple::Simple() :
t("")
{
}
Simple::~Simple()
{
}
void Simple::set(std::string text)
{
t = text;
}
std::string Simple::get(void)
{
return t;
}
template <typename T>
const int get_n_bits()
{
return std::numeric_limits<T>::digits + 1;
}
template <typename T>
const char* pluralise(const char* singluar, const char* plural)
{
return (sizeof(T) == 1) ? singluar : plural;
}
template <typename T>
std:stream& show_size(std:stream& stream, const char* type)
{
stream << type << " has ";
stream << get_n_bits<T>() << " bits";
stream << ", has length of ";
stream << sizeof(T);
stream << " ";
stream << pluralise<T>("byte", "bytes");
stream << ".\n";
return stream;
}
int main(int argc, char *argv[])
{
show_size<bool>(std::cout, "bool");
show_size<char>(std::cout, "char");
show_size<short int>(std::cout, "short int");
show_size<int>(std::cout, "int");
show_size<long int>(std::cout, "long int");
show_size<long long>(std::cout, "long long");
show_size<float>(std::cout, "float");
show_size<double>(std::cout, "double");
show_size<long double>(std::cout, "long double");
show_size<wchar_t>(std::cout, "wchar_t");
show_size<Simple>(std::cout, "Simple");
return 0;
}