Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: are "trait" and "meta-function" synonymous?

Or, does trait perhaps refer to a specific way to utilizing meta-functions?

If they are not synonymous, please point me to some examples of traits which are not meta-functions or those of meta-functions which are not traits. An actually working piece of code, perhaps within STL or Boost libraries, would be appreciated rather than a contrived toy example.

I'd like to see how experts in this field of C++ programming use these terminologies. I'm not sure if there are authoritative definitions of them...

Thanks in advance!


Clarification: It's not that I looking for any examples of traits or meta-functions. I've been using tens (if not hundreds) of them at my day job.

Venn0110.svg
"Venn0110". Licensed under Public Domain via Commons.

like image 606
nodakai Avatar asked Nov 16 '25 05:11

nodakai


2 Answers

"Meta" is C++ terminology for template programming. A clear example is the Boost Meta Programming Library (MPL).

In this sense, a meta-function is a function whose domain is not objects but C++ constructs. The common inputs are therefore types, ordinary functions and other templates.

A simple meta-function is for example template<typename T> using Foo = Bar<T, int> which has as its input a type T and as its output a type Foo<T>. Trivial, yes, but ordinary functions can be trivial too.

A trait is a metafunction whose codomain is a constant expression, often boolean. E.g. is_foo<T>::value obviously is boolean. The oldest trait in this sense is sizeof(T) whose codomain is size_t.

like image 146
MSalters Avatar answered Nov 18 '25 17:11

MSalters


The terms are not equivalent.

Meta functions

The term is not defined in the C++ Standard.

There seem to be two main contending definitions:

1) "meta-functions derive/calculate/map-to values or types (based on their arguments/parameters) at compile time", and/or

  • constexpr functions may or may not be included in any given person's definition/conception; there's functional overlap, but much existing writing mentioning meta functions predates or just doesn't also discuss constexpr functions, so it's hard to know whether any definition or examples given deliberately leaves room for or excludes them

2) "meta-functions are functions and/or classes utilising template meta-programming", which specifically means a core of code (the template) may be reused for different parameters, performing some code generation or transformation based thereon

  • it's not necessarily the case that a meta-programming's final product is a compile-time type or constant - it may be a function intended for use with runtime values or data

A small survey of top google results for "meta function c++"

"metafunctions accept types and compile-time constants as parameters and return types/constants. A metafunction, contrary to its name, is a class template."

"code...executed and its result used while...compiling. ...meta-functions can compute two things: values or types"

this article uses "meta function" to refer to class templates yielding compile-time values/types


Traits

The C++ Standard doesn't define "traits", but does define "traits class"es:

17.3.25 [defns.traits] traits class

a class that encapsulates a set of types and functions necessary for class templates and function templates to manipulate objects of types for which they are instantiated [ Note: Traits classes defined in Clauses 21, 22 and 27 are character traits, which provide the character handling support needed by the string and iostream classes. —end note ]

I'd add that traits can have values, as well as types and functions - for example rank exposes ::value. Traits provide types/values offering insight into either the parameter type itself, or the behaviour desired of the system using the trait when that system's working on variables of that type.

The Standard Library's character traits contain example of runtime functionality: for example X::length(p), X::find(p, n, c).

The <type_traits> header in the C++ Standard Library is a good place to get an initial feel for what kind of things traits can be used for.

Traits are traditionally and typically (but now C++11 provides constexpr functions not necessarily) classes, as distinct from functions meta- or otherwise.

A trait might tell you if a parameter T is constant or not, whether it's serialisable, or whether it should be compressed when transmitted via TCP: whatever some other general-purpose code might need to customise its behaviour for the range of types it handles.

Sometimes traits will be provided by the system that uses them - other times they can be supplied by client code wishing to customise its behaviour on a per-type basis.

Traits may deduce things using various tests of the parameter types, or they may be hand-crafted specialisations hard-coding values for specific types.

This ACCU introduction to traits](http://accu.org/index.php/journals/442) is worth reading, and includes this quote from the creator of C++:

Think of a trait as a small object whose main purpose is to carry information used by another object or algorithm to determine "policy" or "implementation details". - Bjarne Stroustrup

Contrasting "meta function" with "traits"

Regardless of which definition of meta function you adopt, the criteria relates to implementation details: when the function can be evaluated, and/or whether it involves code generation/transformation. That contrasts with traits, where the key concept/requirement is the purpose to which they're put, not the common - but far from universal - implementation using template specialisations or instantiations, or any capacity for compile time vs run-time evaluation.

like image 31
Tony Delroy Avatar answered Nov 18 '25 17:11

Tony Delroy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!