SlideShare a Scribd company logo
1 of 10
Download to read offline
C++ decltype Meta
Functions
Creating Type Functions from Overloaded C++
functions.
måndag 13 mars 17
In C++14 we can hide function return types within the function itself. Combined with
decltype this opens up for a different way to write type returning Meta Functions.
struct input_type_1 {};
auto f(const input_type_1&) {
struct result_type_1 {};
return result_type_1();
}
f returns local ”hidden” type
måndag 13 mars 17
Consider this C++14 construct. Function f returns local (hidden) type.
struct input_type_1 {};
auto f(const input_type_1&) {
struct result_type_1 {};
return result_type_1();
}
template <typename T>
using F = decltype(f(T()));
Meta Function F
As the return type of f
måndag 13 mars 17
This allows us to define a Meta Function F that creates the type returned by f.
struct input_type_1 {};
struct input_type_2 {};
auto f(const input_type_1&) {
struct result_type_1 {};
return result_type_1();
}
auto f(const input_type_2&) {
struct result_type_2 {};
return result_type_2();
}
template <typename T>
using F = decltype(f(T()));
F<input_type_1> is result_type_1
F<input_type_2> is result_type_2
måndag 13 mars 17
We can now use f overloading to compute different return types.
struct input_type_1 {};
auto f(const input_type_1&) {
struct result_type_1 {};
return result_type_1();
}
template <typename T>
using F = decltype(f(T()));
Pro:
F<T> result encapsulated in f
måndag 13 mars 17
Result of F can now be used only through ”calling” F.
struct input_type_1 {};
namespace detail {
struct result_type_1 {};
template <typename T>
struct F {};
template <>
struct F<input_type_1> {
using type = result_type_1;
};
}
template <typename T>
using F = typename detail::F<T>::type;
Pro:
More direct than template
specialization (right)
struct input_type_1 {};
auto f(const input_type_1&) {
struct result_type_1 {};
return result_type_1();
}
template <typename T>
using F = decltype(f(T()));
Compare
måndag 13 mars 17
Compare with how we normally define Meta Functions using Template Specialization. F as
return type of f is more direct.
struct input_type_1 {};
namespace detail {
struct result_type_1 {};
template <typename T>
struct F {};
template <>
struct F<input_type_1> {
using type = result_type_1;
};
}
template <typename T>
using F = typename detail::F<T>::type;
Pro:
F return type local to f/F
(Template Specialization ”leaks”)
struct input_type_1 {};
auto f(const input_type_1&) {
struct result_type_1 {};
return result_type_1();
}
template <typename T>
using F = decltype(f(T()));
Compare
måndag 13 mars 17
F as template specialization ”leaks” the return type (It is accessible outside F). F as return type
of f is accessible ONLY as return type of f, result of F.
Pro/Con?
F result by function overload rules
template <typename T>
using F = decltype(f(T()));
http://en.cppreference.com/w/cpp/language/overload_resolution
måndag 13 mars 17
F can compute only input types that can differentiate in function overloading. Could be a
good or a bad thing? At least it is interesting?
template <typename T>
using F = typename detail::F<T>::type;
Pro/Con?
F result by Template Specialization
Rules
http://en.cppreference.com/w/cpp/language/template_specialization
måndag 13 mars 17
While in ordinary template specialization we may apply the full range of type computation
rules. They are more powerful, which can be good, but also more complicated and obscure
which can be a bad thing when this power is not required?
struct input_type_1 {};
struct input_type_2 {};
auto f(const input_type_1&) {
struct result_type_1 {};
return result_type_1();
}
auto f(const input_type_2&) {
struct result_type_2 {};
return result_type_2();
}
template <typename T>
using F = decltype(f(T()));
Left to the reader...
Does this way of writing Meta
Functions provide any value?
måndag 13 mars 17
Thats all for now. Question is, can we use Meta Functions as decltype of f return type for
anything meaningful?

More Related Content

Recently uploaded

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Recently uploaded (20)

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

C++ decltype meta functions

  • 1. C++ decltype Meta Functions Creating Type Functions from Overloaded C++ functions. måndag 13 mars 17 In C++14 we can hide function return types within the function itself. Combined with decltype this opens up for a different way to write type returning Meta Functions.
  • 2. struct input_type_1 {}; auto f(const input_type_1&) { struct result_type_1 {}; return result_type_1(); } f returns local ”hidden” type måndag 13 mars 17 Consider this C++14 construct. Function f returns local (hidden) type.
  • 3. struct input_type_1 {}; auto f(const input_type_1&) { struct result_type_1 {}; return result_type_1(); } template <typename T> using F = decltype(f(T())); Meta Function F As the return type of f måndag 13 mars 17 This allows us to define a Meta Function F that creates the type returned by f.
  • 4. struct input_type_1 {}; struct input_type_2 {}; auto f(const input_type_1&) { struct result_type_1 {}; return result_type_1(); } auto f(const input_type_2&) { struct result_type_2 {}; return result_type_2(); } template <typename T> using F = decltype(f(T())); F<input_type_1> is result_type_1 F<input_type_2> is result_type_2 måndag 13 mars 17 We can now use f overloading to compute different return types.
  • 5. struct input_type_1 {}; auto f(const input_type_1&) { struct result_type_1 {}; return result_type_1(); } template <typename T> using F = decltype(f(T())); Pro: F<T> result encapsulated in f måndag 13 mars 17 Result of F can now be used only through ”calling” F.
  • 6. struct input_type_1 {}; namespace detail { struct result_type_1 {}; template <typename T> struct F {}; template <> struct F<input_type_1> { using type = result_type_1; }; } template <typename T> using F = typename detail::F<T>::type; Pro: More direct than template specialization (right) struct input_type_1 {}; auto f(const input_type_1&) { struct result_type_1 {}; return result_type_1(); } template <typename T> using F = decltype(f(T())); Compare måndag 13 mars 17 Compare with how we normally define Meta Functions using Template Specialization. F as return type of f is more direct.
  • 7. struct input_type_1 {}; namespace detail { struct result_type_1 {}; template <typename T> struct F {}; template <> struct F<input_type_1> { using type = result_type_1; }; } template <typename T> using F = typename detail::F<T>::type; Pro: F return type local to f/F (Template Specialization ”leaks”) struct input_type_1 {}; auto f(const input_type_1&) { struct result_type_1 {}; return result_type_1(); } template <typename T> using F = decltype(f(T())); Compare måndag 13 mars 17 F as template specialization ”leaks” the return type (It is accessible outside F). F as return type of f is accessible ONLY as return type of f, result of F.
  • 8. Pro/Con? F result by function overload rules template <typename T> using F = decltype(f(T())); http://en.cppreference.com/w/cpp/language/overload_resolution måndag 13 mars 17 F can compute only input types that can differentiate in function overloading. Could be a good or a bad thing? At least it is interesting?
  • 9. template <typename T> using F = typename detail::F<T>::type; Pro/Con? F result by Template Specialization Rules http://en.cppreference.com/w/cpp/language/template_specialization måndag 13 mars 17 While in ordinary template specialization we may apply the full range of type computation rules. They are more powerful, which can be good, but also more complicated and obscure which can be a bad thing when this power is not required?
  • 10. struct input_type_1 {}; struct input_type_2 {}; auto f(const input_type_1&) { struct result_type_1 {}; return result_type_1(); } auto f(const input_type_2&) { struct result_type_2 {}; return result_type_2(); } template <typename T> using F = decltype(f(T())); Left to the reader... Does this way of writing Meta Functions provide any value? måndag 13 mars 17 Thats all for now. Question is, can we use Meta Functions as decltype of f return type for anything meaningful?