See blog post for more information.
Project is released under MIT licence.
My previous entry on traits in C++ was a reasonable start but it missed one important corner case—namely classic C++ enums—and adopted the nonstandard approach of parameterizing the algorithm class on the traits type as opposed to the operand class. Here is a revised sample that addresses these two points:
cpp-traits-v2 | |
cpp-traits-v2.exe | |
See blog post for more information.
Project is released under MIT licence.
#pragma once | |
template<typename Tree> struct OMTraits; | |
template<typename Tree> | |
class Algorithm | |
{ | |
public: | |
typedef typename OMTraits<Tree>::Node Node; | |
typedef typename OMTraits<Tree>::NodeType NodeType; | |
static const NodeType NodeType_TERMINAL = OMTraits<Tree>::NodeType_TERMINAL; | |
static const NodeType NodeType_NONTERMINAL = OMTraits<Tree>::NodeType_NONTERMINAL; | |
void dumpTree(const Tree &tree) const | |
{ | |
cout << "Tree: " << tree.name() << endl; | |
for (typename vector<Node>::const_iterator i = tree.nodes().begin(); i != tree.nodes().end(); ++i) | |
{ | |
dumpNode(*i); | |
} | |
} | |
private: | |
void dumpNode(const Node &node) const | |
{ | |
cout << " " << node.name() << ": " << getNodeTypeString(node.type()) << endl; | |
} | |
string getNodeTypeString(NodeType nodeType) const | |
{ | |
switch (nodeType) | |
{ | |
case NodeType_TERMINAL: return "TERMINAL"; | |
case NodeType_NONTERMINAL: return "NONTERMINAL"; | |
default: return "(other)"; | |
} | |
} | |
}; | |
#pragma once | |
#include <string> | |
#include <iostream> | |
#include <vector> | |
using namespace std; | |
The MIT License (MIT) | |
Copyright (c) 2014 Richard Cook | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
#include "common.hpp" | |
#include "om1.hpp" | |
#include "om2.hpp" | |
#include "algorithm.hpp" | |
template<> | |
struct OMTraits<OM1::Tree> | |
{ | |
typedef OM1::Node Node; | |
typedef OM1::NodeType NodeType; | |
// BEGIN: Generated static constants for enums | |
static const OM1::NodeType NodeType_TERMINAL = OM1::TERMINAL; | |
static const OM1::NodeType NodeType_NONTERMINAL = OM1::NONTERMINAL; | |
// END: Generated static constants for enums | |
}; | |
template<> | |
struct OMTraits<OM2::Tree> | |
{ | |
typedef OM2::Node Node; | |
typedef OM2::NodeType NodeType; | |
// BEGIN: Generated static constants for enums | |
static const OM2::NodeType NodeType_TERMINAL = OM2::TERMINAL; | |
static const OM2::NodeType NodeType_NONTERMINAL = OM2::NONTERMINAL; | |
// END: Generated static constants for enums | |
}; | |
// Partial method specialization for type-specific behaviours | |
template<> | |
void Algorithm<OM2::Tree>::dumpNode(const Node &node) const | |
{ | |
cout << " [specialized behaviour] " << node.name() << ": " << getNodeTypeString(node.type()) << endl; | |
} | |
int main() | |
{ | |
OM1::Tree tree1("OM1::Tree"); | |
tree1.addNode(OM1::Node("OM1::Node1", OM1::TERMINAL)); | |
tree1.addNode(OM1::Node("OM1::Node2", OM1::NONTERMINAL)); | |
Algorithm<OM1::Tree> algorithm1; | |
algorithm1.dumpTree(tree1); | |
OM2::Tree tree2("OM2::Tree"); | |
tree2.addNode(OM2::Node("OM2::Node1", OM2::TERMINAL)); | |
tree2.addNode(OM2::Node("OM2::Node2", OM2::NONTERMINAL)); | |
Algorithm<OM2::Tree> algorithm2; | |
algorithm2.dumpTree(tree2); | |
return 0; | |
} | |
CPP := g++ | |
TARGET := cpp-traits-v2 | |
all: $(TARGET) | |
$(TARGET): main.cpp *.hpp | |
$(CPP) -o $@ main.cpp | |
.PHONY: clean | |
clean: | |
rm -f $(TARGET) | |
#pragma once | |
namespace OM1 | |
{ | |
// Same enum member names as ObjectModel2::NodeType but different values | |
enum NodeType { TERMINAL = 100, NONTERMINAL = 101 }; | |
class Node | |
{ | |
public: | |
Node(const string &name, NodeType type) : m_name(name), m_type(type) { } | |
const string &name() const { return m_name; } | |
NodeType type() const { return m_type; } | |
private: | |
string m_name; | |
NodeType m_type; | |
}; | |
class Tree | |
{ | |
public: | |
Tree(const string &name) : m_name(name) { } | |
const string &name() const { return m_name; } | |
const vector<Node> &nodes() const { return m_nodes; } | |
void addNode(const Node &node) { m_nodes.push_back(node); } | |
private: | |
string m_name; | |
vector<Node> m_nodes; | |
}; | |
} | |
#pragma once | |
namespace OM2 | |
{ | |
// Same enum member names as ObjectModel1::NodeType but different values | |
enum NodeType { TERMINAL = 200, NONTERMINAL = 201 }; | |
class Node | |
{ | |
public: | |
Node(const string &name, NodeType type) : m_name(name), m_type(type) { } | |
const string &name() const { return m_name; } | |
NodeType type() const { return m_type; } | |
private: | |
string m_name; | |
NodeType m_type; | |
}; | |
class Tree | |
{ | |
public: | |
Tree(const string &name) : m_name(name) { } | |
const string &name() const { return m_name; } | |
const vector<Node> &nodes() const { return m_nodes; } | |
void addNode(const Node &node) { m_nodes.push_back(node); } | |
private: | |
string m_name; | |
vector<Node> m_nodes; | |
}; | |
} | |
Class C++ enums suffer from a name scoping issue which complicates traits a little: enum members result in names that are injected into the enclosing namespace and that are not scoped by the name of the enum type itself. This means that we cannot refer to these constants in algorithm code via the enum typedef. I scratched my head about how to overcome this difficulty and, in the end, the only solution I could come up with was to emit static consts for each member of each enum into the partially specialized traits classes. In my particular case, these enums correspond to code that is generated by a tool, so I was simply able to modify the tool to generate part of the partial template specializations, hence the source code comments.
Note that this problem is solved by “enum classes” in C++11 where the members are scoped within the name of the enum itself. When using this language feature, we can remove the static consts altogether and refer to the members via the enum’s typedef.
Most real-world examples of traits usage in C++ parameterize the algorithm class on the operand type as opposed to the traits type itself. Hence, this updated sample declares template<typename Tree> class Algorithm
as opposed to template<typename Traits> class Algorithm
in my original posting.
Content © 2025 Richard Cook. All rights reserved.