Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ traits question

Tags:

c++

traits

I have a templated class

template <typename Data>
class C
{
.....
}

In most situations, I depend on the compiler to let me substitute types for Data. I call methods foo(), goo() on objects of type Data, so what I substitute needs to provide that.

I now need to substitute int and string for my Data type. I do not want to specialize because the class is already too big and would require specializing each method (with only small code change).

My options (please tell me if there are more)

1) I can provide wrapper classes around int and string which implement the methods foo(), goo() etc

2) provide a traits class traits that calls foo() or goo() on objects of classes that provide foo(),goo() (these are my present substitutable classes) and specialize these classes for int and string.

Questions

1) what are the relative merits of 1 vs 2?

2) My traits classes will have static methods. Can a traits class have non-static methods as well? I see most traits classes define constants in the STL.

3) Do I make the traits classes global or should I pass them in as a template parameter for class C?

like image 453
duli Avatar asked Jun 10 '10 20:06

duli


People also ask

What is an C personality type?

What is a C Type Personality? C Type Personality Styles, based on DISC Theory by Dr. Marston, are accurate, precise, detail-oriented, and conscientious. They think analytically and systematically, and carefully make decisions with plenty of research and information to back it up.

What is a high C personality?

Overview of the Conscientious (C) Personality Style People who are high in “C” are more introverted and reserved, and task-oriented. They tend to be cautious, calculating, competent, contemplative, and careful. They are typically analytical, detail-oriented, and intentional.

Who gave Type C personality?

The characteristics of Type C and its relations to other personality types and traits were also the subject of the work of Eysenck (1991).

What are the negative side or weaknesses of type C personality people?

It can have a negative effect on your relationships. Difficulty expressing your own emotions can also make it challenging to understand the emotions and body language of others. You might often think other people are angry or irritated when they aren't, for example. It can also affect your health.


1 Answers

You could specialize part of class like follows:

template <typename Data>
class C
{
  void foo();

  // lot of other stuff
};

// specialize part of class C 
// (some members of a class C will have specific 
//  implementation for specific types)
template<> void C<int>::foo() { std::cout << "int" << std::endl; }
template<> void C<std::string>::foo() { std::cout << "string" << std::endl; }
// same for goo

The syntax above allowed by C++ Standard 14.7/3 and 14.5.2/2. There's no need to rewrite all stuff from class C several times.

Note, that it is not allowed to partially specialize template class in such way. For instance, you cannot define different functions for Data and Data* types in this way.

like image 101
Kirill V. Lyadvinsky Avatar answered Oct 19 '22 23:10

Kirill V. Lyadvinsky