<!DOCTYPE html>

<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="mobile-web-app-capable" content="yes">
    <title>
        function_ref: a type-erased callable reference - HackMD
    </title>
    <link rel="icon" type="image/png" href="https://hackmd.io/favicon.png">
    <link rel="apple-touch-icon" href="https://hackmd.io/apple-touch-icon.png">

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha256-916EbMg70RQy9LHiGkXzG8hSg9EdNy97GazNG/aiY1w=" crossorigin="anonymous" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha256-eZrrJcwDc/3uDhsdt61sL2oOBY362qM3lon1gyExkL0=" crossorigin="anonymous" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css" integrity="sha256-3iu9jgsy9TpTwXKb7bNQzqWekRX7pPK+2OLj3R922fo=" crossorigin="anonymous" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/octicons/3.5.0/octicons.min.css" integrity="sha256-QiWfLIsCT02Sdwkogf6YMiQlj4NE84MKkzEMkZnMGdg=" crossorigin="anonymous" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.5.1/themes/prism.min.css" integrity="sha256-vtR0hSWRc3Tb26iuN2oZHt3KRUomwTufNIf5/4oeCyg=" crossorigin="anonymous" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@hackmd/emojify.js@2.1.0/dist/css/basic/emojify.min.css" integrity="sha256-UOrvMOsSDSrW6szVLe8ZDZezBxh5IoIfgTwdNDgTjiU=" crossorigin="anonymous" />
    <style>
        @import url(https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,500,500i|Source+Code+Pro:300,400,500|Source+Sans+Pro:300,300i,400,400i,600,600i|Source+Serif+Pro&subset=latin-ext);.hljs{background:#fff;color:#333;display:block;overflow-x:auto;padding:.5em}.hljs-comment,.hljs-meta{color:#969896}.hljs-emphasis,.hljs-quote,.hljs-string,.hljs-strong,.hljs-template-variable,.hljs-variable{color:#df5000}.hljs-keyword,.hljs-selector-tag,.hljs-type{color:#a71d5d}.hljs-attribute,.hljs-bullet,.hljs-literal,.hljs-number,.hljs-symbol{color:#0086b3}.hljs-built_in,.hljs-builtin-name{color:#005cc5}.hljs-name,.hljs-section{color:#63a35c}.hljs-tag{color:#333}.hljs-attr,.hljs-selector-attr,.hljs-selector-class,.hljs-selector-id,.hljs-selector-pseudo,.hljs-title{color:#795da3}.hljs-addition{background-color:#eaffea;color:#55a532}.hljs-deletion{background-color:#ffecec;color:#bd2c00}.hljs-link{text-decoration:underline}.markdown-body{word-wrap:break-word;font-size:16px;line-height:1.5}.markdown-body:after,.markdown-body:before{content:"";display:table}.markdown-body:after{clear:both}.markdown-body>:first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdown-body a:not([href]){color:inherit;text-decoration:none}.markdown-body .absent{color:#c00}.markdown-body .anchor{float:left;line-height:1;margin-left:-20px;padding-right:4px}.markdown-body .anchor:focus{outline:none}.markdown-body blockquote,.markdown-body dl,.markdown-body ol,.markdown-body p,.markdown-body pre,.markdown-body table,.markdown-body ul{margin-bottom:16px;margin-top:0}.markdown-body hr{background-color:#e7e7e7;border:0;height:.25em;margin:24px 0;padding:0}.markdown-body blockquote{border-left:.25em solid #ddd;color:#777;font-size:16px;padding:0 1em}.markdown-body blockquote>:first-child{margin-top:0}.markdown-body blockquote>:last-child{margin-bottom:0}.markdown-body kbd,.popover kbd{background-color:#fcfcfc;border:1px solid;border-color:#ccc #ccc #bbb;border-radius:3px;box-shadow:inset 0 -1px 0 #bbb;color:#555;display:inline-block;font-size:11px;line-height:10px;padding:3px 5px;vertical-align:middle}.markdown-body .loweralpha{list-style-type:lower-alpha}.markdown-body h1,.markdown-body h2,.markdown-body h3,.markdown-body h4,.markdown-body h5,.markdown-body h6{font-weight:600;line-height:1.25;margin-bottom:16px;margin-top:24px}.markdown-body h1 .octicon-link,.markdown-body h2 .octicon-link,.markdown-body h3 .octicon-link,.markdown-body h4 .octicon-link,.markdown-body h5 .octicon-link,.markdown-body h6 .octicon-link{color:#000;vertical-align:middle;visibility:hidden}.markdown-body h1:hover .anchor,.markdown-body h2:hover .anchor,.markdown-body h3:hover .anchor,.markdown-body h4:hover .anchor,.markdown-body h5:hover .anchor,.markdown-body h6:hover .anchor{text-decoration:none}.markdown-body h1:hover .anchor .octicon-link,.markdown-body h2:hover .anchor .octicon-link,.markdown-body h3:hover .anchor .octicon-link,.markdown-body h4:hover .anchor .octicon-link,.markdown-body h5:hover .anchor .octicon-link,.markdown-body h6:hover .anchor .octicon-link{visibility:visible}.markdown-body h1 code,.markdown-body h1 tt,.markdown-body h2 code,.markdown-body h2 tt,.markdown-body h3 code,.markdown-body h3 tt,.markdown-body h4 code,.markdown-body h4 tt,.markdown-body h5 code,.markdown-body h5 tt,.markdown-body h6 code,.markdown-body h6 tt{font-size:inherit}.markdown-body h1{font-size:2em}.markdown-body h1,.markdown-body h2{border-bottom:1px solid #eee;padding-bottom:.3em}.markdown-body h2{font-size:1.5em}.markdown-body h3{font-size:1.25em}.markdown-body h4{font-size:1em}.markdown-body h5{font-size:.875em}.markdown-body h6{color:#777;font-size:.85em}.markdown-body ol,.markdown-body ul{padding-left:2em}.markdown-body ol.no-list,.markdown-body ul.no-list{list-style-type:none;padding:0}.markdown-body ol ol,.markdown-body ol ul,.markdown-body ul ol,.markdown-body ul ul{margin-bottom:0;margin-top:0}.markdown-body li>p{margin-top:16px}.markdown-body li+li{padding-top:.25em}.markdown-body dl{padding:0}.markdown-body dl dt{font-size:1em;font-style:italic;font-weight:700;margin-top:16px;padding:0}.markdown-body dl dd{margin-bottom:16px;padding:0 16px}.markdown-body table{display:block;overflow:auto;width:100%;word-break:normal;word-break:keep-all}.markdown-body table th{font-weight:700}.markdown-body table td,.markdown-body table th{border:1px solid #ddd;padding:6px 13px}.markdown-body table tr{background-color:#fff;border-top:1px solid #ccc}.markdown-body table tr:nth-child(2n){background-color:#f8f8f8}.markdown-body img{background-color:#fff;box-sizing:initial;max-width:100%}.markdown-body img[align=right]{padding-left:20px}.markdown-body img[align=left]{padding-right:20px}.markdown-body .emoji{background-color:initial;max-width:none;vertical-align:text-top}.markdown-body span.frame{display:block;overflow:hidden}.markdown-body span.frame>span{border:1px solid #ddd;display:block;float:left;margin:13px 0 0;overflow:hidden;padding:7px;width:auto}.markdown-body span.frame span img{display:block;float:left}.markdown-body span.frame span span{clear:both;color:#333;display:block;padding:5px 0 0}.markdown-body span.align-center{clear:both;display:block;overflow:hidden}.markdown-body span.align-center>span{display:block;margin:13px auto 0;overflow:hidden;text-align:center}.markdown-body span.align-center span img{margin:0 auto;text-align:center}.markdown-body span.align-right{clear:both;display:block;overflow:hidden}.markdown-body span.align-right>span{display:block;margin:13px 0 0;overflow:hidden;text-align:right}.markdown-body span.align-right span img{margin:0;text-align:right}.markdown-body span.float-left{display:block;float:left;margin-right:13px;overflow:hidden}.markdown-body span.float-left span{margin:13px 0 0}.markdown-body span.float-right{display:block;float:right;margin-left:13px;overflow:hidden}.markdown-body span.float-right>span{display:block;margin:13px auto 0;overflow:hidden;text-align:right}.markdown-body code,.markdown-body tt{background-color:#0000000a;border-radius:3px;font-size:85%;margin:0;padding:.2em 0}.markdown-body code:after,.markdown-body code:before,.markdown-body tt:after,.markdown-body tt:before{content:"\00a0";letter-spacing:-.2em}.markdown-body code br,.markdown-body tt br{display:none}.markdown-body del code{text-decoration:inherit}.markdown-body pre{word-wrap:normal}.markdown-body pre>code{background:#0000;border:0;font-size:100%;margin:0;padding:0;white-space:pre;word-break:normal}.markdown-body .highlight{margin-bottom:16px}.markdown-body .highlight pre{margin-bottom:0;word-break:normal}.markdown-body .highlight pre,.markdown-body pre{background-color:#f7f7f7;border-radius:3px;font-size:85%;line-height:1.45;overflow:auto;padding:16px}.markdown-body pre code,.markdown-body pre tt{word-wrap:normal;background-color:initial;border:0;display:inline;line-height:inherit;margin:0;max-width:auto;overflow:visible;padding:0}.markdown-body pre code:after,.markdown-body pre code:before,.markdown-body pre tt:after,.markdown-body pre tt:before{content:normal}.markdown-body .csv-data td,.markdown-body .csv-data th{font-size:12px;line-height:1;overflow:hidden;padding:5px;text-align:left;white-space:nowrap}.markdown-body .csv-data .blob-line-num{background:#fff;border:0;padding:10px 8px 9px;text-align:right}.markdown-body .csv-data tr{border-top:0}.markdown-body .csv-data th{background:#f8f8f8;border-top:0;font-weight:700}.news .alert .markdown-body blockquote{border:0;padding:0 0 0 40px}.activity-tab .news .alert .commits,.activity-tab .news .markdown-body blockquote{padding-left:0}.task-list-item{list-style-type:none}.task-list-item label{font-weight:400}.task-list-item.enabled label{cursor:pointer}.task-list-item+.task-list-item{margin-top:3px}.task-list-item-checkbox{cursor:default!important;float:left;margin:.31em 0 .2em -1.3em!important;vertical-align:middle}.markdown-body{max-width:758px;overflow:visible!important;padding-bottom:40px;padding-top:40px;position:relative}.markdown-body .emoji{vertical-align:top}.markdown-body pre{border:inherit!important}.markdown-body code{color:inherit!important}.markdown-body pre code .wrapper{display:-moz-inline-flex;display:-ms-inline-flex;display:-o-inline-flex;display:inline-flex}.markdown-body pre code .gutter{float:left;overflow:hidden;-webkit-user-select:none;user-select:none}.markdown-body pre code .gutter.linenumber{border-right:3px solid #6ce26c!important;box-sizing:initial;color:#afafaf!important;cursor:default;display:inline-block;min-width:20px;padding:0 8px 0 0;position:relative;text-align:right;z-index:4}.markdown-body pre code .gutter.linenumber>span:before{content:attr(data-linenumber)}.markdown-body pre code .code{float:left;margin:0 0 0 16px}.markdown-body .gist .line-numbers{border-bottom:none;border-left:none;border-top:none}.markdown-body .gist .line-data{border:none}.markdown-body .gist table{border-collapse:inherit!important;border-spacing:0}.markdown-body code[data-gist-id]{background:none;padding:0}.markdown-body code[data-gist-id]:after,.markdown-body code[data-gist-id]:before{content:""}.markdown-body code[data-gist-id] .blob-num{border:unset}.markdown-body code[data-gist-id] table{margin-bottom:unset;overflow:unset}.markdown-body code[data-gist-id] table tr{background:unset}.markdown-body[dir=rtl] pre{direction:ltr}.markdown-body[dir=rtl] code{direction:ltr;unicode-bidi:embed}.markdown-body .alert>p:last-child{margin-bottom:0}.markdown-body pre.abc,.markdown-body pre.flow-chart,.markdown-body pre.graphviz,.markdown-body pre.mermaid,.markdown-body pre.sequence-diagram,.markdown-body pre.vega{background-color:inherit;border-radius:0;overflow:visible;text-align:center;white-space:inherit}.markdown-body pre.abc>code,.markdown-body pre.flow-chart>code,.markdown-body pre.graphviz>code,.markdown-body pre.mermaid>code,.markdown-body pre.sequence-diagram>code,.markdown-body pre.vega>code{text-align:left}.markdown-body pre.abc>svg,.markdown-body pre.flow-chart>svg,.markdown-body pre.graphviz>svg,.markdown-body pre.mermaid>svg,.markdown-body pre.sequence-diagram>svg,.markdown-body pre.vega>svg{height:100%;max-width:100%}.markdown-body pre>code.wrap{word-wrap:break-word;white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap}.markdown-body .alert>p:last-child,.markdown-body .alert>ul:last-child{margin-bottom:0}.markdown-body summary{display:list-item}.markdown-body summary:focus{outline:none}.markdown-body details summary{cursor:pointer}.markdown-body details:not([open])>:not(summary){display:none}.markdown-body figure{margin:1em 40px}.markdown-body .mark,.markdown-body mark{background-color:#fff1a7}.vimeo,.youtube{background-color:#000;background-position:50%;background-repeat:no-repeat;background-size:contain;cursor:pointer;display:table;overflow:hidden;text-align:center}.vimeo,.youtube{position:relative;width:100%}.youtube{padding-bottom:56.25%}.vimeo img{object-fit:contain;width:100%;z-index:0}.youtube img{object-fit:cover;z-index:0}.vimeo iframe,.youtube iframe,.youtube img{height:100%;left:0;position:absolute;top:0;width:100%}.vimeo iframe,.youtube iframe{vertical-align:middle;z-index:1}.vimeo .icon,.youtube .icon{color:#fff;height:auto;left:50%;opacity:.3;position:absolute;top:50%;transform:translate(-50%,-50%);transition:opacity .2s;width:auto;z-index:0}.vimeo:hover .icon,.youtube:hover .icon{opacity:.6;transition:opacity .2s}.slideshare .inner,.speakerdeck .inner{position:relative;width:100%}.slideshare .inner iframe,.speakerdeck .inner iframe{bottom:0;height:100%;left:0;position:absolute;right:0;top:0;width:100%}.figma{display:table;padding-bottom:56.25%;position:relative;width:100%}.figma iframe{border:1px solid #eee;bottom:0;height:100%;left:0;position:absolute;right:0;top:0;width:100%}.markmap-container{height:300px}.markmap-container>svg{height:100%;width:100%}.MJX_Assistive_MathML{display:none}#MathJax_Message{z-index:1000!important}.ui-infobar{color:#777;margin:25px auto -25px;max-width:760px;position:relative;z-index:2}.toc .invisable-node{list-style-type:none}.ui-toc{bottom:20px;position:fixed;z-index:998}.ui-toc.both-mode{margin-left:8px}.ui-toc.both-mode .ui-toc-label{border-bottom-left-radius:0;border-top-left-radius:0;height:40px;padding:10px 4px}.ui-toc-label{background-color:#e6e6e6;border:none;color:#868686;transition:opacity .2s}.ui-toc .open .ui-toc-label{color:#fff;opacity:1;transition:opacity .2s}.ui-toc-label:focus{background-color:#ccc;color:#000;opacity:.3}.ui-toc-label:hover{background-color:#ccc;opacity:1;transition:opacity .2s}.ui-toc-dropdown{margin-bottom:20px;margin-top:20px;max-height:70vh;max-width:45vw;overflow:auto;padding-left:10px;padding-right:10px;text-align:inherit;width:25vw}.ui-toc-dropdown>.toc{max-height:calc(70vh - 100px);overflow:auto}.ui-toc-dropdown[dir=rtl] .nav{letter-spacing:.0029em;padding-right:0}.ui-toc-dropdown a{overflow:hidden;text-overflow:ellipsis;white-space:pre}.ui-toc-dropdown .nav>li>a{color:#767676;display:block;font-size:13px;font-weight:500;padding:4px 20px}.ui-toc-dropdown .nav>li:first-child:last-child>ul,.ui-toc-dropdown .toc.expand ul{display:block}.ui-toc-dropdown .nav>li>a:focus,.ui-toc-dropdown .nav>li>a:hover{background-color:initial;border-left:1px solid #000;color:#000;padding-left:19px;text-decoration:none}.ui-toc-dropdown[dir=rtl] .nav>li>a:focus,.ui-toc-dropdown[dir=rtl] .nav>li>a:hover{border-left:none;border-right:1px solid #000;padding-right:19px}.ui-toc-dropdown .nav>.active:focus>a,.ui-toc-dropdown .nav>.active:hover>a,.ui-toc-dropdown .nav>.active>a{background-color:initial;border-left:2px solid #000;color:#000;font-weight:700;padding-left:18px}.ui-toc-dropdown[dir=rtl] .nav>.active:focus>a,.ui-toc-dropdown[dir=rtl] .nav>.active:hover>a,.ui-toc-dropdown[dir=rtl] .nav>.active>a{border-left:none;border-right:2px solid #000;padding-right:18px}.ui-toc-dropdown .nav .nav{display:none;padding-bottom:10px}.ui-toc-dropdown .nav>.active>ul{display:block}.ui-toc-dropdown .nav .nav>li>a{font-size:12px;font-weight:400;padding-bottom:1px;padding-left:30px;padding-top:1px}.ui-toc-dropdown[dir=rtl] .nav .nav>li>a{padding-right:30px}.ui-toc-dropdown .nav .nav>li>ul>li>a{font-size:12px;font-weight:400;padding-bottom:1px;padding-left:40px;padding-top:1px}.ui-toc-dropdown[dir=rtl] .nav .nav>li>ul>li>a{padding-right:40px}.ui-toc-dropdown .nav .nav>li>a:focus,.ui-toc-dropdown .nav .nav>li>a:hover{padding-left:29px}.ui-toc-dropdown[dir=rtl] .nav .nav>li>a:focus,.ui-toc-dropdown[dir=rtl] .nav .nav>li>a:hover{padding-right:29px}.ui-toc-dropdown .nav .nav>li>ul>li>a:focus,.ui-toc-dropdown .nav .nav>li>ul>li>a:hover{padding-left:39px}.ui-toc-dropdown[dir=rtl] .nav .nav>li>ul>li>a:focus,.ui-toc-dropdown[dir=rtl] .nav .nav>li>ul>li>a:hover{padding-right:39px}.ui-toc-dropdown .nav .nav>.active:focus>a,.ui-toc-dropdown .nav .nav>.active:hover>a,.ui-toc-dropdown .nav .nav>.active>a{font-weight:500;padding-left:28px}.ui-toc-dropdown[dir=rtl] .nav .nav>.active:focus>a,.ui-toc-dropdown[dir=rtl] .nav .nav>.active:hover>a,.ui-toc-dropdown[dir=rtl] .nav .nav>.active>a{padding-right:28px}.ui-toc-dropdown .nav .nav>.active>.nav>.active:focus>a,.ui-toc-dropdown .nav .nav>.active>.nav>.active:hover>a,.ui-toc-dropdown .nav .nav>.active>.nav>.active>a{font-weight:500;padding-left:38px}.ui-toc-dropdown[dir=rtl] .nav .nav>.active>.nav>.active:focus>a,.ui-toc-dropdown[dir=rtl] .nav .nav>.active>.nav>.active:hover>a,.ui-toc-dropdown[dir=rtl] .nav .nav>.active>.nav>.active>a{padding-right:38px}.markdown-body{font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica Neue,Helvetica,Roboto,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol}html[lang^=ja] .markdown-body{font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica Neue,Helvetica,Roboto,Arial,Hiragino Kaku Gothic Pro,ヒラギノ角ゴ Pro W3,Osaka,Meiryo,メイリオ,MS Gothic,ＭＳ ゴシック,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol}html[lang=zh-tw] .markdown-body{font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica Neue,Helvetica,Roboto,Arial,PingFang TC,Microsoft JhengHei,微軟正黑,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol}html[lang=zh-cn] .markdown-body{font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica Neue,Helvetica,Roboto,Arial,PingFang SC,Microsoft YaHei,微软雅黑,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol}html .markdown-body[lang^=ja]{font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica Neue,Helvetica,Roboto,Arial,Hiragino Kaku Gothic Pro,ヒラギノ角ゴ Pro W3,Osaka,Meiryo,メイリオ,MS Gothic,ＭＳ ゴシック,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol}html .markdown-body[lang=zh-tw]{font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica Neue,Helvetica,Roboto,Arial,PingFang TC,Microsoft JhengHei,微軟正黑,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol}html .markdown-body[lang=zh-cn]{font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica Neue,Helvetica,Roboto,Arial,PingFang SC,Microsoft YaHei,微软雅黑,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol}html[lang^=ja] .ui-toc-dropdown{font-family:Source Sans Pro,Helvetica,Arial,Meiryo UI,MS PGothic,ＭＳ Ｐゴシック,sans-serif}html[lang=zh-tw] .ui-toc-dropdown{font-family:Source Sans Pro,Helvetica,Arial,Microsoft JhengHei UI,微軟正黑UI,sans-serif}html[lang=zh-cn] .ui-toc-dropdown{font-family:Source Sans Pro,Helvetica,Arial,Microsoft YaHei UI,微软雅黑UI,sans-serif}html .ui-toc-dropdown[lang^=ja]{font-family:Source Sans Pro,Helvetica,Arial,Meiryo UI,MS PGothic,ＭＳ Ｐゴシック,sans-serif}html .ui-toc-dropdown[lang=zh-tw]{font-family:Source Sans Pro,Helvetica,Arial,Microsoft JhengHei UI,微軟正黑UI,sans-serif}html .ui-toc-dropdown[lang=zh-cn]{font-family:Source Sans Pro,Helvetica,Arial,Microsoft YaHei UI,微软雅黑UI,sans-serif}.ui-affix-toc{max-height:70vh;max-width:15vw;overflow:auto;position:fixed;top:0}.back-to-top,.expand-toggle,.go-to-bottom{color:#999;display:block;font-size:12px;font-weight:500;margin-left:10px;margin-top:10px;padding:4px 10px}.back-to-top:focus,.back-to-top:hover,.expand-toggle:focus,.expand-toggle:hover,.go-to-bottom:focus,.go-to-bottom:hover{color:#563d7c;text-decoration:none}.back-to-top,.go-to-bottom{margin-top:0}.ui-user-icon{background-position:50%;background-repeat:no-repeat;background-size:cover;border-radius:50%;display:block;height:20px;margin-bottom:2px;margin-right:5px;margin-top:2px;width:20px}.ui-user-icon.small{display:inline-block;height:18px;margin:0 0 .2em;vertical-align:middle;width:18px}.ui-infobar>small>span{line-height:22px}.ui-infobar>small .dropdown{display:inline-block}.ui-infobar>small .dropdown a:focus,.ui-infobar>small .dropdown a:hover{text-decoration:none}.ui-more-info{color:#888;cursor:pointer;vertical-align:middle}.ui-more-info .fa{font-size:16px}.ui-connectedGithub,.ui-published-note{color:#888}.ui-connectedGithub{line-height:23px;white-space:nowrap}.ui-connectedGithub a.file-path{color:#888;padding-left:22px;text-decoration:none}.ui-connectedGithub a.file-path:active,.ui-connectedGithub a.file-path:hover{color:#888;text-decoration:underline}.ui-connectedGithub .fa{font-size:20px}.ui-published-note .fa{font-size:20px;vertical-align:top}.unselectable{-webkit-user-select:none;-o-user-select:none;user-select:none}.selectable{-webkit-user-select:text;-o-user-select:text;user-select:text}.inline-spoiler-section{cursor:pointer}.inline-spoiler-section .spoiler-text{background-color:#333;border-radius:2px}.inline-spoiler-section .spoiler-text>*{opacity:0}.inline-spoiler-section .spoiler-img{filter:blur(10px)}.inline-spoiler-section.raw{background-color:#333;border-radius:2px}.inline-spoiler-section.raw>*{opacity:0}.inline-spoiler-section.unveil{cursor:auto}.inline-spoiler-section.unveil .spoiler-text{background-color:#3333331a}.inline-spoiler-section.unveil .spoiler-text>*{opacity:1}.inline-spoiler-section.unveil .spoiler-img{filter:none}@media print{blockquote,div,img,pre,table{page-break-inside:avoid!important}a[href]:after{font-size:12px!important}}.markdown-body.slides{color:#222;position:relative;z-index:1}.markdown-body.slides:before{background-color:currentColor;bottom:0;box-shadow:0 0 0 50vw;content:"";display:block;left:0;position:absolute;right:0;top:0;z-index:-1}.markdown-body.slides section[data-markdown]{background-color:#fff;margin-bottom:1.5em;position:relative;text-align:center}.markdown-body.slides section[data-markdown] code{text-align:left}.markdown-body.slides section[data-markdown]:before{content:"";display:block;padding-bottom:56.23%}.markdown-body.slides section[data-markdown]>div:first-child{left:1em;max-height:100%;overflow:hidden;position:absolute;right:1em;top:50%;transform:translateY(-50%)}.markdown-body.slides section[data-markdown]>ul{display:inline-block}.markdown-body.slides>section>section+section:after{border:3px solid #777;content:"";height:1.5em;position:absolute;right:1em;top:-1.5em}.site-ui-font{font-family:Source Sans Pro,Helvetica,Arial,sans-serif}html[lang^=ja] .site-ui-font{font-family:Source Sans Pro,Helvetica,Arial,Hiragino Kaku Gothic Pro,ヒラギノ角ゴ Pro W3,Osaka,Meiryo,メイリオ,MS Gothic,ＭＳ ゴシック,sans-serif}html[lang=zh-tw] .site-ui-font{font-family:Source Sans Pro,Helvetica,Arial,PingFang TC,Microsoft JhengHei,微軟正黑,sans-serif}html[lang=zh-cn] .site-ui-font{font-family:Source Sans Pro,Helvetica,Arial,PingFang SC,Microsoft YaHei,微软雅黑,sans-serif}body{font-smoothing:subpixel-antialiased!important;-webkit-font-smoothing:subpixel-antialiased!important;-moz-osx-font-smoothing:auto!important;-webkit-overflow-scrolling:touch;font-family:Source Sans Pro,Helvetica,Arial,sans-serif;letter-spacing:.025em}html[lang^=ja] body{font-family:Source Sans Pro,Helvetica,Arial,Hiragino Kaku Gothic Pro,ヒラギノ角ゴ Pro W3,Osaka,Meiryo,メイリオ,MS Gothic,ＭＳ ゴシック,sans-serif}html[lang=zh-tw] body{font-family:Source Sans Pro,Helvetica,Arial,PingFang TC,Microsoft JhengHei,微軟正黑,sans-serif}html[lang=zh-cn] body{font-family:Source Sans Pro,Helvetica,Arial,PingFang SC,Microsoft YaHei,微软雅黑,sans-serif}abbr[title]{border-bottom:none;text-decoration:underline;-webkit-text-decoration:underline dotted;text-decoration:underline dotted}abbr[data-original-title],abbr[title]{cursor:help}body.modal-open{overflow-y:auto;padding-right:0!important}svg{text-shadow:none}
    </style>
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    	<script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js" integrity="sha256-3Jy/GbSLrg0o9y5Z5n1uw0qxZECH7C6OQpVBgNFYa0g=" crossorigin="anonymous"></script>
    	<script src="https://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.min.js" integrity="sha256-g6iAfvZp+nDQ2TdTR/VVKJf3bGro4ub5fvWSWVRi2NE=" crossorigin="anonymous"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.9/es5-shim.min.js" integrity="sha256-8E4Is26QH0bD52WoQpcB+R/tcWQtpzlCojrybUd7Mxo=" crossorigin="anonymous"></script>
    <![endif]-->
</head>

<body>
    <div id="doc" class="markdown-body container-fluid comment-enabled" data-hard-breaks="false"><style>
ins { background-color: #CCFFCC }
s { background-color: #FFCACA }
blockquote { color: inherit !important }
</style><table><tbody>
<tr><th>Doc. no.:</th>    <td>P0792R12</td></tr>
<tr><th>Date:</th>        <td>2022-11-28</td></tr>
<tr><th>Audience:</th>    <td>LWG</td></tr>
<tr><th>Reply-to:</th>    <td>Vittorio Romeo &lt;vittorio.romeo@outlook.com&gt;<br>
Zhihao Yuan &lt;zy@miator.net&gt;<br>
Jarrad Waterloo &lt;descender76@gmail.com&gt;</td></tr>
</tbody></table><h1 id="function_ref-a-type-erased-callable-reference" data-id="function_ref-a-type-erased-callable-reference"><a class="anchor hidden-xs" href="#function_ref-a-type-erased-callable-reference" title="function_ref-a-type-erased-callable-reference"><span class="octicon octicon-link"></span></a><code>function_ref</code><span>: a type-erased callable reference</span></h1><h2 id="Table-of-contents" data-id="Table-of-contents"><a class="anchor hidden-xs" href="#Table-of-contents" title="Table-of-contents"><span class="octicon octicon-link"></span></a><span>Table of contents</span></h2><p><span class="toc"><ul>
<li><a href="#function_ref-a-type-erased-callable-reference" title="function_ref: a type-erased callable reference">function_ref: a type-erased callable reference</a><ul>
<li><a href="#Table-of-contents" title="Table of contents">Table of contents</a></li>
<li><a href="#Changelog" title="Changelog">Changelog</a></li>
<li><a href="#Abstract" title="Abstract">Abstract</a></li>
<li><a href="#Motivating-examples" title="Motivating examples">Motivating examples</a><ul>
<li><a href="#Using-function-pointers" title="Using function pointers">Using function pointers</a></li>
<li><a href="#Using-a-template" title="Using a template">Using a template</a></li>
<li><a href="#Using-stdfunction-or-stdmove_only_function" title="Using std::function or std::move_only_function">Using std::function or std::move_only_function</a></li>
<li><a href="#Using-the-proposed-function_ref" title="Using the proposed function_ref">Using the proposed function_ref</a></li>
</ul>
</li>
<li><a href="#Design-considerations" title="Design considerations">Design considerations</a><ul>
<li><a href="#Survey" title="Survey">Survey</a></li>
<li><a href="#Additional-information" title="Additional information">Additional information</a></li>
</ul>
</li>
<li><a href="#Design-decisions" title="Design decisions">Design decisions</a></li>
<li><a href="#Wording" title="Wording">Wording</a></li>
<li><a href="#Feature-test-macro" title="Feature test macro">Feature test macro</a></li>
<li><a href="#Implementation-Experience" title="Implementation Experience">Implementation Experience</a></li>
<li><a href="#Acknowledgments" title="Acknowledgments">Acknowledgments</a></li>
<li><a href="#References" title="References">References</a></li>
</ul>
</li>
</ul>
</span></p><h2 id="Changelog" data-id="Changelog"><a class="anchor hidden-xs" href="#Changelog" title="Changelog"><span class="octicon octicon-link"></span></a><span>Changelog</span></h2><h4 id="R12" data-id="R12"><a class="anchor hidden-xs" href="#R12" title="R12"><span class="octicon octicon-link"></span></a><span>R12</span></h4><ul>
<li><span>Adjust wording per LWG review (2022-11);</span></li>
<li><span>Correct a deduction guide that mishandled pointer to data member.</span></li>
</ul><h4 id="R11" data-id="R11"><a class="anchor hidden-xs" href="#R11" title="R11"><span class="octicon octicon-link"></span></a><span>R11</span></h4><ul>
<li><span>Fix oversights and typos in the wording.</span></li>
</ul><h4 id="R10" data-id="R10"><a class="anchor hidden-xs" href="#R10" title="R10"><span class="octicon octicon-link"></span></a><span>R10</span></h4><ul>
<li><span>Integrate the </span><a href="#Additional-information"><code>nontype_t</code></a><span> constructors.</span></li>
</ul><h4 id="R9" data-id="R9"><a class="anchor hidden-xs" href="#R9" title="R9"><span class="octicon octicon-link"></span></a><span>R9</span></h4><ul>
<li><span>Declare the main template as variadic for future extension;</span></li>
<li><span>Allow declaring a variable of </span><code>function_ref</code><span> </span><code>constinit</code><span>.</span></li>
</ul><h4 id="R8" data-id="R8"><a class="anchor hidden-xs" href="#R8" title="R8"><span class="octicon octicon-link"></span></a><span>R8</span></h4><ul>
<li><span>Stop supporting pointer-to-members;</span></li>
<li><span>Prevent assigning from callable objects other than function pointers while retaining copy assignment;</span></li>
<li><span>Consolidate the wording with better terminologies.</span></li>
</ul><h4 id="R7" data-id="R7"><a class="anchor hidden-xs" href="#R7" title="R7"><span class="octicon octicon-link"></span></a><span>R7</span></h4><ul>
<li><span>Clarify the proposal to handle function and function pointers in the same way.</span></li>
</ul><h4 id="R6" data-id="R6"><a class="anchor hidden-xs" href="#R6" title="R6"><span class="octicon octicon-link"></span></a><span>R6</span></h4><ul>
<li><span>Avoid double-wrapping existing references to callables;</span></li>
<li><span>Reworked the wording to follow the latest standardese;</span></li>
<li><span>Applied changes requested by LWG (2020-07);</span></li>
<li><span>Removed a deduction guide that is incompatible with explicit object parameters.</span></li>
</ul><h4 id="R5" data-id="R5"><a class="anchor hidden-xs" href="#R5" title="R5"><span class="octicon octicon-link"></span></a><span>R5</span></h4><ul>
<li><span>Removed “qualifiers” from </span><code>operator()</code><span> specification (typo);</span></li>
</ul><h4 id="R4" data-id="R4"><a class="anchor hidden-xs" href="#R4" title="R4"><span class="octicon octicon-link"></span></a><span>R4</span></h4><ul>
<li><span>Removed </span><code>constexpr</code><span> due to implementation concerns;</span></li>
<li><span>Explicitly say that the type is trivially copyable;</span></li>
<li><span>Added brief before synopsis;</span></li>
<li><span>Reworded specification following P1369.</span></li>
</ul><h4 id="R3" data-id="R3"><a class="anchor hidden-xs" href="#R3" title="R3"><span class="octicon octicon-link"></span></a><span>R3</span></h4><ul>
<li><span>Constructing or assigning from </span><code>std::function</code><span> no longer has a precondition;</span></li>
<li><code>function_ref::operator()</code><span> is now unconditionally </span><code>const</code><span>-qualified.</span></li>
</ul><h4 id="R2" data-id="R2"><a class="anchor hidden-xs" href="#R2" title="R2"><span class="octicon octicon-link"></span></a><span>R2</span></h4><ul>
<li><span>Made copy constructor and assignment operator </span><code>= default</code><span>;</span></li>
<li><span>Added </span><em><span>exposition only</span></em><span> data members.</span></li>
</ul><h4 id="R1" data-id="R1"><a class="anchor hidden-xs" href="#R1" title="R1"><span class="octicon octicon-link"></span></a><span>R1</span></h4><ul>
<li><span>Removed empty state, comparisons with </span><code>nullptr</code><span>, and default constructor;</span></li>
<li><span>Added support for </span><code>noexcept</code><span> and </span><code>const</code><span>-qualified function signatures;</span></li>
<li><span>Added deduction guides similar to </span><code>std::function</code><span>;</span></li>
<li><span>Added example implementation;</span></li>
<li><span>Added feature test macro;</span></li>
<li><span>Removed </span><code>noexcept</code><span> from constructor and assignment operator.</span></li>
</ul><h2 id="Abstract" data-id="Abstract"><a class="anchor hidden-xs" href="#Abstract" title="Abstract"><span class="octicon octicon-link"></span></a><span>Abstract</span></h2><p><span>This paper proposes the addition of </span><code>function_ref&lt;R(Args...)&gt;</code><span>, a </span><em><span>vocabulary type</span></em><span> with reference semantics for passing entities to call, to the standard library.</span></p><h2 id="Motivating-examples" data-id="Motivating-examples"><a class="anchor hidden-xs" href="#Motivating-examples" title="Motivating-examples"><span class="octicon octicon-link"></span></a><span>Motivating examples</span></h2><p><span>Here’s one example use case that benefits from </span><em><span>higher-order functions</span></em><span>: a </span><code>retry(n, f)</code><span> function that attempts to call </span><code>f</code><span> up to </span><code>n</code><span> times synchronously until success. This example might model the real-world scenario of repeatedly querying a flaky web service.</span></p><pre><code class="cpp hljs"><span class="token keyword">using</span> payload <span class="token operator">=</span> std<span class="token double-colon punctuation">::</span>optional<span class="token operator">&lt;</span> <span class="token comment">/* ... */</span> <span class="token operator">&gt;</span><span class="token punctuation">;</span>

<span class="token comment">// Repeatedly invokes `action` up to `times` repetitions.</span>
<span class="token comment">// Immediately returns if `action` returns a valid `payload`.</span>
<span class="token comment">// Returns `std::nullopt` otherwise.</span>
payload <span class="token function">retry</span><span class="token punctuation">(</span>size_t times<span class="token punctuation">,</span> <span class="token comment">/* ????? */</span> action<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p><span>The passed-in </span><code>action</code><span> should be a callable entity that takes no arguments and returns a </span><code>payload</code><span>. Let’s see how to implemented </span><code>retry</code><span> with various techniques.</span></p><h3 id="Using-function-pointers" data-id="Using-function-pointers"><a class="anchor hidden-xs" href="#Using-function-pointers" title="Using-function-pointers"><span class="octicon octicon-link"></span></a><span>Using function pointers</span></h3><pre><code class="cpp hljs">payload <span class="token function">retry</span><span class="token punctuation">(</span>size_t times<span class="token punctuation">,</span> <span class="token function">payload</span><span class="token punctuation">(</span><span class="token operator">*</span>action<span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">/* ... */</span>
<span class="token punctuation">}</span>
</code></pre><ul>
<li>
<p><strong><span>Advantages</span></strong><span>:</span></p>
<ul>
<li>
<p><span>Easy to implement: no template, nor constraint. The function pointer type has a signature that nails down which functions to pass.</span></p>
</li>
<li>
<p><span>Minimal overhead: no allocations, no exceptions, and major calling conventions can pass a function pointer in a register.</span></p>
</li>
</ul>
</li>
<li>
<p><strong><span>Drawbacks</span></strong><span>:</span></p>
<ul>
<li><span>A function is usually not stateful, nor does captureless closure objects. One cannot pass other function objects in C++ this way.</span></li>
</ul>
</li>
</ul><h3 id="Using-a-template" data-id="Using-a-template"><a class="anchor hidden-xs" href="#Using-a-template" title="Using-a-template"><span class="octicon octicon-link"></span></a><span>Using a template</span></h3><pre><code class="cpp hljs"><span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">F</span><span class="token operator">&gt;</span>
<span class="token keyword">auto</span> <span class="token function">retry</span><span class="token punctuation">(</span>size_t times<span class="token punctuation">,</span> F<span class="token operator">&amp;&amp;</span> action<span class="token punctuation">)</span>
<span class="token keyword">requires</span> std<span class="token double-colon punctuation">::</span>is_invocable_r_v<span class="token operator">&lt;</span>payload<span class="token punctuation">,</span> F<span class="token operator">&gt;</span>
<span class="token punctuation">{</span>
    <span class="token comment">/* ... */</span>
<span class="token punctuation">}</span>
</code></pre><ul>
<li>
<p><strong><span>Advantages</span></strong><span>:</span></p>
<ul>
<li>
<p><span>Support arbitrary function objects, such as closures with captures.</span></p>
</li>
<li>
<p><span>Zero-overhead: no allocations, no exceptions, no indirections.</span></p>
</li>
</ul>
</li>
<li>
<p><strong><span>Drawbacks</span></strong><span>:</span></p>
<ul>
<li>
<p><span>Harder to implement: users must constrain </span><code>action</code><span>’s signature.</span></p>
</li>
<li>
<p><span>Fail to support separable compilation: the implementation of </span><code>retry</code><span> must appear in a header file. A slight change at the call site will cause recompilation of the function body.</span></p>
</li>
</ul>
</li>
</ul><h3 id="Using-stdfunction-or-stdmove_only_function" data-id="Using-stdfunction-or-stdmove_only_function"><a class="anchor hidden-xs" href="#Using-stdfunction-or-stdmove_only_function" title="Using-stdfunction-or-stdmove_only_function"><span class="octicon octicon-link"></span></a><span>Using </span><code>std::function</code><span> or </span><code>std::move_only_function</code></h3><pre><code class="cpp hljs">payload <span class="token function">retry</span><span class="token punctuation">(</span>size_t times<span class="token punctuation">,</span> std<span class="token double-colon punctuation">::</span>move_only_function<span class="token operator">&lt;</span><span class="token function">payload</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&gt;</span> action<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">/* ... */</span>
<span class="token punctuation">}</span>
</code></pre><ul>
<li>
<p><strong><span>Advantages</span></strong><span>:</span></p>
<ul>
<li>
<p><span>Take more </span><em><span>callable objects</span></em><span>, from closures to pointer-to-members.</span></p>
</li>
<li>
<p><span>Easy to implement: no need to use a template or any explicit constraint. </span><code>std::function</code><span> and </span><code>std::move_only_function</code><span> constructor is constrained.</span></p>
</li>
</ul>
</li>
<li>
<p><strong><span>Drawbacks</span></strong><span>:</span></p>
<ul>
<li>
<p><code>std::function</code><span> and </span><code>std::move_only_function</code><span>’s converting constructor</span><sup class="footnote-ref"><a href="#fn1" id="fnref1">[1]</a></sup><span> require their target objects to be copy-constructible or move-constructible, respectively;</span></p>
</li>
<li>
<p><span>Comes with potentially significant overhead:</span></p>
<ul>
<li>
<p><span>The call wrappers start to allocate the target objects when they do not fit in a small buffer, introducing more indirection when calling the objects.</span></p>
</li>
<li>
<p><span>No calling conventions can pass these call wrappers in registers.</span></p>
</li>
<li>
<p><span>Modern compilers cannot inline these call wrappers, often resulting in inferior codegen then previously mentioned techniques.</span></p>
</li>
</ul>
</li>
</ul>
<p><span>One rarely known technique is to pass callable objects to call wrappers via a </span><code>std::reference_wrapper</code><span>:</span></p>
<pre><code class="cpp hljs"><span class="token keyword">auto</span> result <span class="token operator">=</span> <span class="token function">retry</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> std<span class="token double-colon punctuation">::</span><span class="token function">ref</span><span class="token punctuation">(</span>downloader<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p><span>But users cannot replace the </span><code>downloader</code><span> in the example with a lambda expression as such an expression is an rvalue. Meanwhile, all the machinery that implements type-erased copying or moving must still be present in the codegen.</span></p>
</li>
</ul><h3 id="Using-the-proposed-function_ref" data-id="Using-the-proposed-function_ref"><a class="anchor hidden-xs" href="#Using-the-proposed-function_ref" title="Using-the-proposed-function_ref"><span class="octicon octicon-link"></span></a><span>Using the proposed </span><code>function_ref</code></h3><pre><code class="cpp hljs">payload <span class="token function">retry</span><span class="token punctuation">(</span>size_t times<span class="token punctuation">,</span> function_ref<span class="token operator">&lt;</span><span class="token function">payload</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&gt;</span> action<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">/* ... */</span>
<span class="token punctuation">}</span>
</code></pre><ul>
<li>
<p><strong><span>Advantages</span></strong><span>:</span></p>
<ul>
<li>
<p><span>Takes any </span><em><span>callable objects</span></em><span> regardless of whether they are constructible.</span></p>
</li>
<li>
<p><span>Easy to implement: no need to use a template or any constraint. </span><code>function_ref</code><span> is constrained.</span></p>
</li>
<li>
<p><span>Clean ownership semantics: </span><code>function_ref</code><span> has reference semantics as its name suggests.</span></p>
</li>
<li>
<p><span>Minimal overhead: no allocations, no exceptions, certain calling conventions can pass </span><code>function_ref</code><span> in registers.</span></p>
<ul>
<li><span>Modern compilers can perform tail-call optimization when generating thunks. If the function body is visible, they can deliver optimal codegen, identical to the template solution.</span></li>
</ul>
</li>
</ul>
</li>
</ul><h2 id="Design-considerations" data-id="Design-considerations"><a class="anchor hidden-xs" href="#Design-considerations" title="Design-considerations"><span class="octicon octicon-link"></span></a><span>Design considerations</span></h2><p><span>This paper went through LEWG at R5, with a number of consensuses reached and applied to the wording:</span></p><ol>
<li><span>Do not provide </span><code>target()</code><span> or </span><code>target_type</code><span>;</span></li>
<li><span>Do not provide </span><code>operator bool</code><span>, default constructor, or comparison with </span><code>nullptr</code><span>;</span></li>
<li><span>Provide </span><code>R(Args...) noexcept</code><span> specializations;</span></li>
<li><span>Provide </span><code>R(Args...) const</code><span> specializations;</span></li>
<li><span>Require the target entity to be </span><em><span>Lvalue-Callable</span></em><span>;</span></li>
<li><span>Make </span><code>operator()</code><span> unconditionally </span><code>const</code><span>;</span></li>
<li><span>Choose </span><code>function_ref</code><span> as the right name.</span></li>
</ol><p><span>One design question remains not fully understood by many: how should a function pointer initialize </span><code>function_ref</code><span>?</span></p><p><span>In a typical scenario, there is no lifetime issue no matter whether the </span><code>download</code><span> entity below is a function, a function pointer, or a closure:</span></p><pre><code class="cpp hljs"><span class="token keyword">auto</span> result <span class="token operator">=</span> <span class="token function">retry</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> download<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// always safe</span>
</code></pre><p><span>However, even if the users use </span><code>function_ref</code><span> only as parameters initially, it’s not uncommon to evolve the API by grouping parameters into structures,</span></p><pre><code class="cpp hljs"><span class="token keyword">struct</span> <span class="token class-name">retry_options</span>
<span class="token punctuation">{</span>
    size_t times<span class="token punctuation">;</span>
    function_ref<span class="token operator">&lt;</span><span class="token function">payload</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&gt;</span> action<span class="token punctuation">;</span>
    seconds step_back<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

payload <span class="token function">retry</span><span class="token punctuation">(</span>retry_options<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* ... */</span>

<span class="token keyword">auto</span> result <span class="token operator">=</span> <span class="token function">retry</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">.</span>times <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">,</span>
                     <span class="token punctuation">.</span>action <span class="token operator">=</span> download<span class="token punctuation">,</span>
                     <span class="token punctuation">.</span>step_back <span class="token operator">=</span> <span class="token number">1.5</span>s<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

</code></pre><p><span>and structures start to need constructors or factories to simplify initialization:</span></p><pre><code class="cpp hljs"><span class="token keyword">auto</span> opt <span class="token operator">=</span> <span class="token function">default_strategy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
opt<span class="token punctuation">.</span>action <span class="token operator">=</span> download<span class="token punctuation">;</span>
<span class="token keyword">auto</span> result <span class="token operator">=</span> <span class="token function">retry</span><span class="token punctuation">(</span>opt<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p><span>According to the P0792R5</span><sup class="footnote-ref"><a href="#fn2" id="fnref2">[2]</a></sup><span> wording, the code has well-defined behavior if </span><code>download</code><span> is a function. However, one </span><strong><span>cannot</span></strong><span> write the code as</span></p><pre><code class="cpp hljs"><span class="token keyword">auto</span> opt <span class="token operator">=</span> <span class="token function">default_strategy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
opt<span class="token punctuation">.</span>action <span class="token operator">=</span> <span class="token operator">&amp;</span>download<span class="token punctuation">;</span>
<span class="token keyword">auto</span> result <span class="token operator">=</span> <span class="token function">retry</span><span class="token punctuation">(</span>opt<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p><span>since this will create a </span><code>function_ref</code><span> with a dangling object pointer that points to a temporary object – the function pointer.</span></p><p><span>In other words, the following code also has undefined behavior:</span></p><pre><code class="cpp hljs"><span class="token keyword">auto</span> opt <span class="token operator">=</span> <span class="token function">default_strategy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
opt<span class="token punctuation">.</span>action <span class="token operator">=</span> ssh<span class="token punctuation">.</span><span class="token function">get_download_callback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// a function pointer</span>
<span class="token keyword">auto</span> result <span class="token operator">=</span> <span class="token function">retry</span><span class="token punctuation">(</span>opt<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p><span>The users have to write the following to get well-defined behavior.</span></p><pre><code class="cpp hljs"><span class="token keyword">auto</span> opt <span class="token operator">=</span> <span class="token function">default_strategy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
opt<span class="token punctuation">.</span>action <span class="token operator">=</span> <span class="token operator">*</span>ssh<span class="token punctuation">.</span><span class="token function">get_download_callback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">auto</span> result <span class="token operator">=</span> <span class="token function">retry</span><span class="token punctuation">(</span>opt<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><h3 id="Survey" data-id="Survey"><a class="anchor hidden-xs" href="#Survey" title="Survey"><span class="octicon octicon-link"></span></a><span>Survey</span></h3><p><span>We collected the following </span><code>function_ref</code><span> implementations available today:</span></p><p><span style="color: rgb(9, 99, 125)"><span>■</span></span><a href="https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/ADT/STLExtras.h" target="_blank" rel="noopener"><code>llvm::function_ref</code></a><span> – from LLVM</span><sup class="footnote-ref"><a href="#fn3" id="fnref3">[3]</a></sup></p><p><span style="color: rgb(132, 190, 106);"><span>■</span></span><a href="https://github.com/TartanLlama/function_ref/blob/master/include/tl/function_ref.hpp" target="_blank" rel="noopener"><code>tl::function_ref</code></a><span> – by Sy Brand</span></p><p><span style="color: rgb(43, 57, 144);"><span>■</span></span><a href="https://github.com/facebook/folly/blob/main/folly/Function.h" target="_blank" rel="noopener"><code>folly::FunctionRef</code></a><span> – from Meta</span></p><p><span style="color: rgb(255, 207, 171);"><span>■</span></span><a href="https://github.com/bminor/binutils-gdb/blob/master/gdbsupport/function-view.h" target="_blank" rel="noopener"><code>gdb::function_view</code></a><span> – from GNU</span></p><p><span style="color: rgb(158, 16, 32);"><span>■</span></span><a href="https://github.com/foonathan/type_safe/blob/main/include/type_safe/reference.hpp" target="_blank" rel="noopener"><code>type_safe::function_ref</code></a><span> – by Jonathan Müller</span><sup class="footnote-ref"><a href="#fn4" id="fnref4">[4]</a></sup></p><p><span style="color: rgb(227, 178, 0);"><span>■</span></span><a href="https://github.com/abseil/abseil-cpp/blob/master/absl/functional/function_ref.h" target="_blank" rel="noopener"><code>absl::function_ref</code></a><span> – from Google</span></p><p><span>They have diverging behaviors when initialized from function pointers:</span></p><table>
<caption>Behavior A.1: Stores a function pointer if initialized from a function, stores a pointer to function pointer if initialized from a function pointer</caption>
<thead>
<tr><th>Outcome</th><th>Library</th></tr>
</thead>
<tbody>
<tr>
<td>
<p><span>Undefined:</span></p>
<pre><code class="cpp hljs">opt<span class="token punctuation">.</span>action <span class="token operator">=</span> ssh<span class="token punctuation">.</span><span class="token function">get_download_callback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p><span>Well-defined:</span></p>
<pre><code class="cpp hljs">opt<span class="token punctuation">.</span>action <span class="token operator">=</span> download<span class="token punctuation">;</span>
</code></pre>
</td>
<td>
<p><span style="color: rgb(9, 99, 125)"><span>■</span></span><code>llvm::function_ref</code></p>
<p><span style="color: rgb(132, 190, 106);"><span>■</span></span><code>tl::function_ref</code></p>
</td></tr>
</tbody></table><table>
<caption>Behavior A.2: Stores a function pointer if initialized from a function or a function pointer</caption>
<thead>
<tr><th>Outcome</th><th>Library</th></tr>
</thead>
<tbody>
<tr>
<td>
<p><span>Well-defined:</span></p>
<pre><code class="cpp hljs">opt<span class="token punctuation">.</span>action <span class="token operator">=</span> ssh<span class="token punctuation">.</span><span class="token function">get_download_callback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p><span>Well-defined:</span></p>
<pre><code class="cpp hljs">opt<span class="token punctuation">.</span>action <span class="token operator">=</span> download<span class="token punctuation">;</span>
</code></pre>
</td>
<td>
<p><span style="color: rgb(43, 57, 144);"><span>■</span></span><code>folly::FunctionRef</code></p>
<p><span style="color: rgb(255, 207, 171);"><span>■</span></span><code>gdb::function_view</code></p>
<p><span style="color: rgb(158, 16, 32);"><span>■</span></span><code>type_safe::function_ref</code></p>
<p><span style="color: rgb(227, 178, 0);"><span>■</span></span><code>absl::function_ref</code></p>
</td></tr>
</tbody></table><p><span>P0792R5 wording gives </span><strong><span>Behavior A.1</span></strong><span>.</span></p><p><span>A related question is what happens when initialized from pointer-to-members. In the following tables, assume </span><code>&amp;Ssh::connect</code><span> is a pointer to member function:</span></p><table>
<caption>Behavior B.1: Stores a pointer to pointer-to-member if initialized from a pointer-to-member</caption>
<thead>
<tr><th>Outcome</th><th>Library</th></tr>
</thead>
<tbody>
<tr>
<td>
<p><span>Well-defined:</span></p>
<pre><code class="cpp hljs">lib<span class="token punctuation">.</span><span class="token function">send_cmd</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>Ssh<span class="token double-colon punctuation">::</span>connect<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p><span>Undefined:</span></p>
<pre><code class="cpp hljs">function_ref<span class="token operator">&lt;</span><span class="token keyword">void</span><span class="token punctuation">(</span>Ssh<span class="token operator">&amp;</span><span class="token punctuation">)</span><span class="token operator">&gt;</span> cmd <span class="token operator">=</span> <span class="token operator">&amp;</span>Ssh<span class="token double-colon punctuation">::</span>connect<span class="token punctuation">;</span>
</code></pre>
</td>
<td>
<p><span style="color: rgb(132, 190, 106);"><span>■</span></span><code>tl::function_ref</code></p>
<p><span style="color: rgb(43, 57, 144);"><span>■</span></span><code>folly::FunctionRef</code></p>
<p><span style="color: rgb(227, 178, 0);"><span>■</span></span><code>absl::function_ref</code></p>
</td></tr>
</tbody></table><table>
<caption>Behavior B.2: Only supports callable entities with function call expression</caption>
<thead>
<tr><th>Outcome</th><th>Library</th></tr>
</thead>
<tbody>
<tr>
<td>
<p><span>Ill-formed:</span></p>
<pre><code class="cpp hljs">lib<span class="token punctuation">.</span><span class="token function">send_cmd</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>Ssh<span class="token double-colon punctuation">::</span>connect<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p><span>Ill-formed:</span></p>
<pre><code class="cpp hljs">function_ref<span class="token operator">&lt;</span><span class="token keyword">void</span><span class="token punctuation">(</span>Ssh<span class="token operator">&amp;</span><span class="token punctuation">)</span><span class="token operator">&gt;</span> cmd <span class="token operator">=</span> <span class="token operator">&amp;</span>Ssh<span class="token double-colon punctuation">::</span>connect<span class="token punctuation">;</span>
</code></pre>
<p><span>Well-defined:</span></p>
<pre><code class="cpp hljs">lib<span class="token punctuation">.</span><span class="token function">send_cmd</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span><span class="token function">mem_fn</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>Ssh<span class="token double-colon punctuation">::</span>connect<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
</td>
<td>
<p><span style="color: rgb(9, 99, 125)"><span>■</span></span><code>llvm::function_ref</code></p>
<p><span style="color: rgb(255, 207, 171);"><span>■</span></span><code>gdb::function_view</code></p>
<p><span style="color: rgb(158, 16, 32);"><span>■</span></span><code>type_safe::function_ref</code></p>
</td></tr>
</tbody></table><p><span>P0792R5-R7 wording gives </span><strong><span>Behavior B.1</span></strong><span>.</span></p><h3 id="Additional-information" data-id="Additional-information"><a class="anchor hidden-xs" href="#Additional-information" title="Additional-information"><span class="octicon octicon-link"></span></a><span>Additional information</span></h3><p><span>P2472 “make </span><code>function_ref</code><span> more functional” </span><sup class="footnote-ref"><a href="#fn5" id="fnref5">[5]</a></sup><span> suggests a way to initialize </span><code>function_ref</code><span> from pointer-to-members without dangling in all contexts:</span></p><pre><code class="cpp hljs">function_ref<span class="token operator">&lt;</span><span class="token keyword">void</span><span class="token punctuation">(</span>Ssh<span class="token operator">&amp;</span><span class="token punctuation">)</span><span class="token operator">&gt;</span> cmd <span class="token operator">=</span> nontype<span class="token operator">&lt;</span><span class="token operator">&amp;</span>Ssh<span class="token double-colon punctuation">::</span>connect<span class="token operator">&gt;</span><span class="token punctuation">;</span>
</code></pre><p><span>Not convertible from pointer-to-members means that </span><code>function_ref</code><span> does not need to use </span><code>invoke_r</code><span> in the implementation, improving debug codegen in specific toolchains with little effort.</span></p><p><span>Making </span><code>function_ref</code><span> large enough to fit a thunk pointer plus any pointer-to-member-function may render </span><code>std::function_ref</code><span> irrelevant in the real world. Some platform ABIs can pass a trivially copyable type of a 2-word size in registers and cannot do the same to a bigger type. Here is some LLVM IR to show the difference:</span>
<a href="https://godbolt.org/z/Ke3475vz8" target="_blank" rel="noopener"><span>https://godbolt.org/z/Ke3475vz8</span></a><span>.</span></p><p><span>For good or bad, the expression </span><code><span>&amp;</span><em><span>qualified-id</span></em></code><span> that retrieves pointer-to-member shares grammar with the expression that gets a pointer to explicit object member functions. </span><a href="https://eel.is/c++draft/expr.unary.op#3" target="_blank" rel="noopener"><span>[expr.unary.op/3]</span></a></p><h2 id="Design-decisions" data-id="Design-decisions"><a class="anchor hidden-xs" href="#Design-decisions" title="Design-decisions"><span class="octicon octicon-link"></span></a><span>Design decisions</span></h2><ul>
<li>
<p><strong><span>Behavior A.2</span></strong><span> is incorporated to eliminate the difference between initializing </span><code>function_ref</code><span> from a function and initializing </span><code>function_ref</code><span> from a function pointer.</span></p>
</li>
<li>
<p><strong><span>Behavior B.2</span></strong><span> is incorporated to reduce potential damage at a small cost. This change will reflect on the constructor’s constraints.</span></p>
</li>
<li>
<p><span>LEWG further requested making converting assignment from anything other than functions and function pointers ill-formed. Note that </span><code>function_ref</code><span> will still be copy-assignable.</span></p>
</li>
<li>
<p><span>LEWG achieved consensus on </span><a href="#Additional-information"><span>P2472R3</span></a><span>, and the wording is merged here. This change brings back the pointer-to-member support in a different form.</span></p>
</li>
</ul><h2 id="Wording" data-id="Wording"><a class="anchor hidden-xs" href="#Wording" title="Wording"><span class="octicon octicon-link"></span></a><span>Wording</span></h2><p><span>The wording is relative to </span><a href="https://wg21.link/N4917" target="_blank" rel="noopener"><span>N4917</span></a><span>.</span></p><p><span>Add new templates to </span><a href="https://eel.is/c++draft/utility.syn" target="_blank" rel="noopener"><span>[utility.syn]</span></a><span>, header </span><code>&lt;utility&gt;</code><span> synopsis, after </span><code>in_place_index_t</code><span> and </span><code>in_place_index</code><span>:</span></p><pre>namespace std {
  [...]

  <ins><i>// nontype argument tag</i>
  template&lt;auto V&gt;
    struct nontype_t {
      explicit nontype_t() = default;
    };

  template&lt;auto V&gt; constexpr nontype_t&lt;V&gt; nontype{};</ins>
}
</pre><p><span>Add the template to </span><a href="https://eel.is/c++draft/functional.syn" target="_blank" rel="noopener"><span>[functional.syn]</span></a><span>, header </span><code>&lt;functional&gt;</code><span> synopsis:</span></p><blockquote>
<p><span>[…]</span></p>
</blockquote><pre>  <i>// [func.wrap.move], move only wrapper</i>
  template&lt;class... S&gt; class move_only_function;        <i>// not defined</i>
  template&lt;class R, class... ArgTypes&gt;
    class move_only_function&lt;R(ArgTypes...) <i>cv ref</i> noexcept(<i>noex</i>)&gt;; <i>// see below</i>

  <ins><i>// [func.wrap.ref], non-owning wrapper</i>
  template&lt;class... S&gt; class function_ref;              <i>// not defined</i>
  template&lt;class R, class... ArgTypes&gt;
    class function_ref&lt;R(ArgTypes...) <i>cv</i> noexcept(<i>noex</i>)&gt;;           <i>// see below</i></ins>
</pre><blockquote>
<p><span>[…]</span></p>
</blockquote><p><span>Create a new section “Non-owning wrapper” </span><code>[func.wrap.ref]</code><span> with the following after </span><code>[func.wrap.move]</code><span>:</span></p><blockquote>
<h2 id="General" data-id="General"><a class="anchor hidden-xs" href="#General" title="General"><span class="octicon octicon-link"></span></a><span>General</span></h2>
<p><span>[func.wrap.ref.general]</span></p>
<p><span>The header provides partial specializations of </span><code>function_ref</code><span> for each combination of the possible replacements of the placeholders </span><em><span>cv</span></em><span> and </span><em><span>noex</span></em><span> where:</span></p>
<ul>
<li><em><span>cv</span></em><span> is either </span><code>const</code><span> or empty.</span></li>
<li><em><span>noex</span></em><span> is either </span><code>true</code><span> or </span><code>false</code><span>.</span></li>
</ul>
</blockquote><br><blockquote>
<h3 id="Class-template-function_ref" data-id="Class-template-function_ref"><a class="anchor hidden-xs" href="#Class-template-function_ref" title="Class-template-function_ref"><span class="octicon octicon-link"></span></a><span>Class template </span><code>function_ref</code></h3>
<p><span>[func.wrap.ref.class]</span></p>
</blockquote><pre>namespace std
{
  template&lt;class... S&gt; class function_ref;    <i>// not defined</i>

  template&lt;class R, class... ArgTypes&gt;
  class function_ref&lt;R(ArgTypes...) <i>cv</i> noexcept(<i>noex</i>)&gt;
  {
  public:
    <i>// [func.wrap.ref.ctor], constructors and assignment operators</i>
    template&lt;class F&gt; function_ref(F*) noexcept;
    template&lt;class F&gt; constexpr function_ref(F&amp;&amp;) noexcept;
    template&lt;auto f&gt; constexpr function_ref(nontype_t&lt;f&gt;) noexcept;
    template&lt;auto f, class U&gt;
      constexpr function_ref(nontype_t&lt;f&gt;, U&amp;&amp;) noexcept;
    template&lt;auto f, class T&gt;
      constexpr function_ref(nontype_t&lt;f&gt;, <i>cv</i> T*) noexcept;

    constexpr function_ref(const function_ref&amp;) noexcept = default;
    constexpr function_ref&amp; operator=(const function_ref&amp;) noexcept = default;
    template&lt;class T&gt; function_ref&amp; operator=(T) = delete;

    <i>// [func.wrap.ref.inv], invocation</i>
    R operator()(ArgTypes...) const noexcept(<i>noex</i>);
  private:
    template&lt;class... T&gt;
      static constexpr bool <i>is-invocable-using</i> = <i>see below</i>;   <i>// exposition only</i>
  };

  <i>// [func.wrap.ref.deduct], deduction guides</i>
  template&lt;class F&gt;
    function_ref(F*) -&gt; function_ref&lt;F&gt;;
  template&lt;auto f&gt;
    function_ref(nontype_t&lt;f&gt;) -&gt; function_ref&lt;<i>see below</i>&gt;;
  template&lt;auto f&gt;
    function_ref(nontype_t&lt;f&gt;, auto) -&gt; function_ref&lt;<i>see below</i>&gt;;
}
</pre><br><blockquote>
<p><span>An object of class </span><code><span>function_ref&lt;R(Args...) </span><em><span>cv</span></em><span> noexcept(</span><em><span>noex</span></em><span>)&gt;</span></code><span> stores a pointer to thunk </span><em><code>thunk-ptr</code></em><span> and a bound entity </span><em><code>bound-entity</code></em><span>.</span>
<span>The bound entity has an unspecified trivially copyable type </span><code>BoundEntityType</code><span>, that models </span><code>copyable</code><span> and is capable of storing a pointer to object value or a pointer to function value.</span>
<span>A thunk is a function of signature </span><code><span>R(BoundEntityType, Args&amp;&amp;...) noexcept(</span><em><span>noex</span></em><span>)</span></code><span>.</span></p>
<p><span>Each specialization of </span><code>function_ref</code><span> is a trivially copyable type </span><a href="https://eel.is/c++draft/basic.types" target="_blank" rel="noopener"><span>[basic.types]</span></a><span>.</span></p>
<p><span>Within this subclause, </span><em><code>call-args</code></em><span> is an argument pack with elements such that </span><code><span>decltype((</span><em><span>call-args</span></em><span>))...</span></code><span> are </span><code><span>Args&amp;&amp;...</span></code><span> respectively.</span></p>
</blockquote><br><br><blockquote>
<h3 id="Constructors-and-assignment-operators" data-id="Constructors-and-assignment-operators"><a class="anchor hidden-xs" href="#Constructors-and-assignment-operators" title="Constructors-and-assignment-operators"><span class="octicon octicon-link"></span></a><span>Constructors and assignment operators</span></h3>
<p><span>[func.wrap.ref.ctor]</span></p>
</blockquote><pre>template&lt;class... T&gt;
  static constexpr bool <i>is-invocable-using</i> = <i>see below</i>;
</pre><blockquote>
<p><span>If </span><em><span>noex</span></em><span> is true, </span><code><i><span>is-invocable-using</span></i><span>&lt;T...&gt;</span></code><span> is equal to:</span></p>
<p><span>&nbsp;&nbsp;</span><code>is_nothrow_invocable_r_v&lt;R, T..., ArgTypes...&gt;</code></p>
<p><span>Otherwise, </span><code><i><span>is-invocable-using</span></i><span>&lt;T...&gt;</span></code><span> is equal to:</span></p>
<p><span>&nbsp;&nbsp;</span><code>is_invocable_r_v&lt;R, T..., ArgTypes...&gt;</code></p>
</blockquote><br><pre><code>template&lt;class F&gt; function_ref(F* f);
</code></pre><blockquote>
<p><em><span>Constraints:</span></em></p>
<ul>
<li><code>is_function_v&lt;F&gt;</code><span> is </span><code>true</code><span>, and</span></li>
<li><code><i><span>is-invocable-using</span></i><span>&lt;F&gt;</span></code><span> is </span><code>true</code><span>.</span></li>
</ul>
<p><em><span>Preconditions</span></em><span>: </span><em><code>f</code></em><span> is not a null pointer.</span></p>
<p><em><span>Effects:</span></em><span> Initializes </span><em><code>bound-entity</code></em><span> with </span><code>f</code><span>, and </span><em><code>thunk-ptr</code></em><span> to the address of a function  </span><em><code>thunk</code></em><span> such that </span><code><em><span>thunk</span></em><span>(</span><em><span>bound-entity</span></em><span>, </span><em><span>call-args</span></em><span>...)</span></code><span> is expression-equivalent to </span><code><span>invoke_r&lt;R&gt;(f, </span><em><span>call-args</span></em><span>...)</span></code><span>.</span></p>
</blockquote><br><pre><code>template&lt;class F&gt; constexpr function_ref(F&amp;&amp; f);
</code></pre><blockquote>
<p><span>Let </span><code>T</code><span> be </span><code>remove_reference_t&lt;F&gt;</code><span>.</span></p>
<p><em><span>Constraints:</span></em></p>
<ul>
<li><code>remove_cvref_t&lt;F&gt;</code><span> is not the same type as </span><code>function_ref</code><span>,</span></li>
<li><code>is_member_pointer_v&lt;T&gt;</code><span> is </span><code>false</code><span>, and</span></li>
<li><code><i><span>is-invocable-using</span></i><span>&lt;</span><i><span>cv</span></i><span> T&amp;&gt;</span></code><span> is </span><code>true</code><span>.</span></li>
</ul>
<p><em><span>Effects:</span></em><span> Initializes </span><em><code>bound-entity</code></em><span> with </span><code>addressof(f)</code><span>, and </span><em><code>thunk-ptr</code></em><span> to address of a function </span><em><code>thunk</code></em><span> such that </span><code><em><span>thunk</span></em><span>(</span><em><span>bound-entity</span></em><span>, </span><em><span>call-args</span></em><span>...)</span></code><span> is expression-equivalent to </span><code><span>invoke_r&lt;R&gt;(static_cast&lt;</span><em><span>cv</span></em><span> T&amp;&gt;(f), </span><em><span>call-args</span></em><span>...)</span></code><span>.</span></p>
</blockquote><br><pre><code>template&lt;auto f&gt; constexpr function_ref(nontype_t&lt;f&gt;) noexcept;
</code></pre><blockquote>
<p><em><span>Constraints:</span></em><span> </span><code><i><span>is-invocable-using</span></i><span>&lt;decltype(f)&gt;</span></code><span> is </span><code>true</code><span>.</span></p>
<p><em><span>Mandates:</span></em><span> If </span><code>is_pointer_v&lt;F&gt; || is_member_pointer_v&lt;F&gt;</code><span> is </span><code>true</code><span>, </span><em><code>f</code></em><span> is not a null pointer.</span></p>
<p><em><span>Effects:</span></em><span> Initializes </span><em><code>bound-entity</code></em><span> with a pointer to unspecified object or null pointer value, and </span><em><code>thunk-ptr</code></em><span> to address of a function </span><em><code>thunk</code></em><span> such that </span><code><em><span>thunk</span></em><span>(</span><em><span>bound-entity</span></em><span>, </span><em><span>call-args</span></em><span>...)</span></code><span> is expression-equivalent to </span><code><span>invoke_r&lt;R&gt;(f, </span><em><span>call-args</span></em><span>...)</span></code><span>.</span></p>
</blockquote><br><pre><code>template&lt;auto f, class U&gt;
  constexpr function_ref(nontype_t&lt;f&gt;, U&amp;&amp; obj) noexcept;
</code></pre><blockquote>
<p><span>Let </span><code>T</code><span> be </span><code>remove_reference_t&lt;U&gt;</code><span>.</span></p>
<p><em><span>Constraints:</span></em></p>
<ul>
<li><code>is_rvalue_reference_v&lt;U&amp;&amp;&gt;</code><span> is </span><code>false</code><span>,</span></li>
<li><code><i><span>is-invocable-using</span></i><span>&lt;decltype(f), </span><i><span>cv</span></i><span> T&amp;&gt;</span></code><span> is </span><code>true</code><span>.</span></li>
</ul>
<p><em><span>Mandates:</span></em><span> If </span><code>is_pointer_v&lt;F&gt; || is_member_pointer_v&lt;F&gt;</code><span> is </span><code>true</code><span>, </span><em><code>f</code></em><span> is not a null pointer.</span></p>
<p><em><span>Effects:</span></em><span> Initializes </span><em><code>bound-entity</code></em><span> with </span><code>addressof(obj)</code><span>, and </span><em><code>thunk-ptr</code></em><span> to address of a function </span><em><code>thunk</code></em><span> such that </span><code><em><span>thunk</span></em><span>(</span><em><span>bound-entity</span></em><span>, </span><em><span>call-args</span></em><span>...)</span></code><span> is expression-equivalent to </span><code><span>invoke_r&lt;R&gt;(f, static_cast&lt;</span><em><span>cv</span></em><span> T&amp;&gt;(obj), </span><em><span>call-args</span></em><span>...)</span></code><span>.</span></p>
</blockquote><br><pre>template&lt;auto f, class T&gt;
  constexpr function_ref(nontype_t&lt;f&gt;, <i>cv</i> T* obj) noexcept;
</pre><blockquote>
<p><em><span>Constraints:</span></em><span> </span><code><i><span>is-invocable-using</span></i><span>&lt;decltype(f), </span><i><span>cv</span></i><span> T*&gt;</span></code><span> is </span><code>true</code><span>.</span></p>
<p><em><span>Mandates:</span></em><span> If </span><code>is_pointer_v&lt;F&gt; || is_member_pointer_v&lt;F&gt;</code><span> is </span><code>true</code><span>, </span><em><code>f</code></em><span> is not a null pointer.</span></p>
<p><em><span>Preconditions</span></em><span>: </span><em><code>obj</code></em><span> is not a null pointer.</span></p>
<p><em><span>Effects:</span></em><span> Initializes </span><em><code>bound-entity</code></em><span> with </span><code>obj</code><span>, and </span><em><code>thunk-ptr</code></em><span> to address of a function </span><em><code>thunk</code></em><span> such that </span><code><em><span>thunk</span></em><span>(</span><em><span>bound-entity</span></em><span>, </span><em><span>call-args</span></em><span>...)</span></code><span> is expression-equivalent to </span><code><span>invoke_r&lt;R&gt;(f, obj, </span><em><span>call-args</span></em><span>...)</span></code><span>.</span></p>
</blockquote><br><pre><code>template&lt;class T&gt; function_ref&amp; operator=(T) = delete;
</code></pre><blockquote>
<p><em><span>Constraints:</span></em></p>
<ul>
<li><code>T</code><span> is not the same type as </span><code>function_ref</code><span>, and</span></li>
<li><code>is_pointer_v&lt;T&gt;</code><span> is </span><code>false</code><span>, and</span></li>
<li><code><i><span>is-invocable-using</span></i><span>&lt;</span><i><span>cv</span></i><span> T&amp;&gt;</span></code><span> is </span><code>true</code><span>.</span></li>
</ul>
</blockquote><br><br><blockquote>
<h3 id="Invocation" data-id="Invocation"><a class="anchor hidden-xs" href="#Invocation" title="Invocation"><span class="octicon octicon-link"></span></a><span>Invocation</span></h3>
<p><span>[func.wrap.ref.inv]</span></p>
</blockquote><pre>R operator()(ArgTypes... args) const noexcept(<i>noex</i>);
</pre><blockquote>
<p><em><span>Effects:</span></em><span> Equivalent to</span>
<code><span>return </span><em><span>thunk-ptr</span></em><span>(</span><em><span>bound-entity</span></em><span>, std::forward&lt;ArgTypes&gt;(args)...);</span></code></p>
</blockquote><br><br><blockquote>
<h3 id="Deduction-guides" data-id="Deduction-guides"><a class="anchor hidden-xs" href="#Deduction-guides" title="Deduction-guides"><span class="octicon octicon-link"></span></a><span>Deduction guides</span></h3>
<p><span>[func.wrap.ref.deduct]</span></p>
</blockquote><pre><code>template&lt;class F&gt;
  function_ref(F*) -&gt; function_ref&lt;F&gt;;
</code></pre><blockquote>
<p><em><span>Constraints:</span></em><span> </span><code>is_function_v&lt;F&gt;</code><span> is </span><code>true</code><span>.</span></p>
</blockquote><br><pre>template&lt;auto f&gt;
  function_ref(nontype_t&lt;f&gt;) -&gt; function_ref&lt;<i>see below</i>&gt;;
</pre><blockquote>
<p><span>Let </span><code>F</code><span> be </span><code>remove_pointer_t&lt;decltype(f)&gt;</code><span>.</span></p>
<p><em><span>Constraints:</span></em><span> </span><code>is_function_v&lt;F&gt;</code><span> is </span><code>true</code><span>.</span></p>
<p><em><span>Remarks:</span></em><span> The deduced type is </span><code>function_ref&lt;F&gt;</code><span>.</span></p>
</blockquote><br><pre>template&lt;auto f, class T&gt;
  function_ref(nontype_t&lt;f&gt;, T&amp;&amp;) -&gt; function_ref&lt;<i>see below</i>&gt;;
</pre><blockquote>
<p><em><span>Constraints:</span></em></p>
<ul>
<li><code>decltype(f)</code><span> is of the form </span><code><span>R(G::*)(A...) </span><i><span>cv</span></i><span> &amp;</span><sub><i><span>opt</span></i></sub><span> noexcept(</span><i><span>E</span></i><span>)</span></code><span> for a type </span><code>G</code><span>, or</span></li>
<li><code>decltype(f)</code><span> is of the form </span><code><span>M G::*</span></code><span> for a type </span><code>G</code><span> and an object type </span><code>M</code><span>, in which case let </span><code>R</code><span> be </span><code>invoke_result_t&lt;decltype(f), T&gt;</code><span>, </span><code>A...</code><span> be an empty pack, and </span><em><code>E</code></em><span> be </span><code>false</code><span>, or</span></li>
<li><code>decltype(f)</code><span> is of the form </span><code><span>R(*)(G, A...) noexcept(</span><i><span>E</span></i><span>)</span></code><span> for a type </span><code>G</code><span>.</span></li>
</ul>
<p><em><span>Remarks:</span></em><span> The deduced type is </span><code><span>function_ref&lt;R(A...) noexcept(</span><i><span>E</span></i><span>)&gt;</span></code><span>.</span></p>
</blockquote><br><h2 id="Feature-test-macro" data-id="Feature-test-macro"><a class="anchor hidden-xs" href="#Feature-test-macro" title="Feature-test-macro"><span class="octicon octicon-link"></span></a><span>Feature test macro</span></h2><blockquote>
<p><span>Insert the following to </span><a href="https://eel.is/c++draft/version.syn" target="_blank" rel="noopener"><span>[version.syn]</span></a><span>, header </span><code>&lt;version&gt;</code><span> synopsis:</span></p>
</blockquote><pre>#define __cpp_lib_function_ref 20XXXXL <i>// also in &lt;functional&gt;</i>
</pre><h2 id="Implementation-Experience" data-id="Implementation-Experience"><a class="anchor hidden-xs" href="#Implementation-Experience" title="Implementation-Experience"><span class="octicon octicon-link"></span></a><span>Implementation Experience</span></h2><p><span>A complete implementation is available from</span>
<span style="color: rgb(185, 208, 240);"><span>■</span></span><span>&nbsp;</span><a href="https://github.com/zhihaoy/nontype_functional/tree/p0792r12" target="_blank" rel="noopener"><span>zhihaoy/nontype_functional@p0792r12</span></a><span>.</span></p><p><span>Many facilities similar to </span><code>function_ref</code><span> exist and are widely used in large codebases. See </span><a href="#Survey"><span>Survey</span></a><span> for details.</span></p><h2 id="Acknowledgments" data-id="Acknowledgments"><a class="anchor hidden-xs" href="#Acknowledgments" title="Acknowledgments"><span class="octicon octicon-link"></span></a><span>Acknowledgments</span></h2><p><span>Thanks to </span><strong><span>Agustín Bergé</span></strong><span>, </span><strong><span>Dietmar Kühl</span></strong><span>, </span><strong><span>Eric Niebler</span></strong><span>, </span><strong><span>Tim van Deurzen</span></strong><span>, and </span><strong><span>Alisdair Meredith</span></strong><span> for providing very valuable feedback on earlier drafts of this proposal.</span></p><p><span>Thanks to </span><strong><span>Jens Maurer</span></strong><span> for encouraging participation and </span><strong><span>Tomasz Kamiński</span></strong><span> for the thorough wording review.</span></p><h2 id="References" data-id="References"><a class="anchor hidden-xs" href="#References" title="References"><span class="octicon octicon-link"></span></a><span>References</span></h2><hr class="footnotes-sep"><section class="footnotes">
<ol class="footnotes-list">
<li id="fn1" class="footnote-item"><p><em><span>move_only_function</span></em>
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0288r9.html" target="_blank" rel="noopener"><span>http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0288r9.html</span></a> <a href="#fnref1" class="footnote-backref">↩︎</a></p>
</li>
<li id="fn2" class="footnote-item"><p><em><span>function_ref: a non-owning reference to a Callable</span></em>
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0792r5.html" target="_blank" rel="noopener"><span>http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0792r5.html</span></a> <a href="#fnref2" class="footnote-backref">↩︎</a></p>
</li>
<li id="fn3" class="footnote-item"><p><span>The function_ref class template.</span>
<em><span>LLVM Programmer’s Manual</span></em>
<a href="https://llvm.org/docs/ProgrammersManual.html#the-function-ref-class-template" target="_blank" rel="noopener"><span>https://llvm.org/docs/ProgrammersManual.html#the-function-ref-class-template</span></a> <a href="#fnref3" class="footnote-backref">↩︎</a></p>
</li>
<li id="fn4" class="footnote-item"><p><em><span>Implementing function_view is harder than you might think</span></em>
<a href="http://foonathan.net/blog/2017/01/20/function-ref-implementation.html" target="_blank" rel="noopener"><span>http://foonathan.net/blog/2017/01/20/function-ref-implementation.html</span></a> <a href="#fnref4" class="footnote-backref">↩︎</a></p>
</li>
<li id="fn5" class="footnote-item"><p><em><span>make function_ref more functional</span></em>
<a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2472r3.html" target="_blank" rel="noopener"><span>https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2472r3.html</span></a> <a href="#fnref5" class="footnote-backref">↩︎</a></p>
</li>
</ol>
</section></div>
    <div class="ui-toc dropup unselectable hidden-print" style="display:none;">
        <div class="pull-right dropdown">
            <a id="tocLabel" class="ui-toc-label btn btn-default" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false" title="Table of content">
                <i class="fa fa-bars"></i>
            </a>
            <ul id="ui-toc" class="ui-toc-dropdown dropdown-menu" aria-labelledby="tocLabel">
                <div class="toc"><ul class="nav">
<li><a href="#function_ref-a-type-erased-callable-reference" title="function_ref: a type-erased callable reference">function_ref: a type-erased callable reference</a><ul class="nav">
<li><a href="#Table-of-contents" title="Table of contents">Table of contents</a></li>
<li><a href="#Changelog" title="Changelog">Changelog</a></li>
<li><a href="#Abstract" title="Abstract">Abstract</a></li>
<li><a href="#Motivating-examples" title="Motivating examples">Motivating examples</a><ul class="nav">
<li><a href="#Using-function-pointers" title="Using function pointers">Using function pointers</a></li>
<li><a href="#Using-a-template" title="Using a template">Using a template</a></li>
<li><a href="#Using-stdfunction-or-stdmove_only_function" title="Using std::function or std::move_only_function">Using std::function or std::move_only_function</a></li>
<li><a href="#Using-the-proposed-function_ref" title="Using the proposed function_ref">Using the proposed function_ref</a></li>
</ul>
</li>
<li><a href="#Design-considerations" title="Design considerations">Design considerations</a><ul class="nav">
<li><a href="#Survey" title="Survey">Survey</a></li>
<li><a href="#Additional-information" title="Additional information">Additional information</a></li>
</ul>
</li>
<li><a href="#Design-decisions" title="Design decisions">Design decisions</a></li>
<li><a href="#Wording" title="Wording">Wording</a></li>
<li><a href="#Feature-test-macro" title="Feature test macro">Feature test macro</a></li>
<li><a href="#Implementation-Experience" title="Implementation Experience">Implementation Experience</a></li>
<li><a href="#Acknowledgments" title="Acknowledgments">Acknowledgments</a></li>
<li><a href="#References" title="References">References</a></li>
</ul>
</li>
</ul>
</div><div class="toc-menu"><a class="expand-toggle" href="#">Expand all</a><a class="back-to-top" href="#">Back to top</a><a class="go-to-bottom" href="#">Go to bottom</a></div>
            </ul>
        </div>
    </div>
    <div id="ui-toc-affix" class="ui-affix-toc ui-toc-dropdown unselectable hidden-print" data-spy="affix" style="top:17px;display:none;" null null>
        <div class="toc"><ul class="nav">
<li><a href="#function_ref-a-type-erased-callable-reference" title="function_ref: a type-erased callable reference">function_ref: a type-erased callable reference</a><ul class="nav">
<li><a href="#Table-of-contents" title="Table of contents">Table of contents</a></li>
<li><a href="#Changelog" title="Changelog">Changelog</a></li>
<li><a href="#Abstract" title="Abstract">Abstract</a></li>
<li><a href="#Motivating-examples" title="Motivating examples">Motivating examples</a><ul class="nav">
<li><a href="#Using-function-pointers" title="Using function pointers">Using function pointers</a></li>
<li><a href="#Using-a-template" title="Using a template">Using a template</a></li>
<li><a href="#Using-stdfunction-or-stdmove_only_function" title="Using std::function or std::move_only_function">Using std::function or std::move_only_function</a></li>
<li><a href="#Using-the-proposed-function_ref" title="Using the proposed function_ref">Using the proposed function_ref</a></li>
</ul>
</li>
<li><a href="#Design-considerations" title="Design considerations">Design considerations</a><ul class="nav">
<li><a href="#Survey" title="Survey">Survey</a></li>
<li><a href="#Additional-information" title="Additional information">Additional information</a></li>
</ul>
</li>
<li><a href="#Design-decisions" title="Design decisions">Design decisions</a></li>
<li><a href="#Wording" title="Wording">Wording</a></li>
<li><a href="#Feature-test-macro" title="Feature test macro">Feature test macro</a></li>
<li><a href="#Implementation-Experience" title="Implementation Experience">Implementation Experience</a></li>
<li><a href="#Acknowledgments" title="Acknowledgments">Acknowledgments</a></li>
<li><a href="#References" title="References">References</a></li>
</ul>
</li>
</ul>
</div><div class="toc-menu"><a class="expand-toggle" href="#">Expand all</a><a class="back-to-top" href="#">Back to top</a><a class="go-to-bottom" href="#">Go to bottom</a></div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha256-U5ZEeKfGNOja007MMD3YBI0A3OSZOQbeG6z2f2Y0hu8=" crossorigin="anonymous" defer></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gist-embed/2.6.0/gist-embed.min.js" integrity="sha256-KyF2D6xPIJUW5sUDSs93vWyZm+1RzIpKCexxElmxl8g=" crossorigin="anonymous" defer></script>
    <script>
        var markdown = $(".markdown-body");
        //smooth all hash trigger scrolling
        function smoothHashScroll() {
            var hashElements = $("a[href^='#']").toArray();
            for (var i = 0; i < hashElements.length; i++) {
                var element = hashElements[i];
                var $element = $(element);
                var hash = element.hash;
                if (hash) {
                    $element.on('click', function (e) {
                        // store hash
                        var hash = this.hash;
                        if ($(hash).length <= 0) return;
                        // prevent default anchor click behavior
                        e.preventDefault();
                        // animate
                        $('body, html').stop(true, true).animate({
                            scrollTop: $(hash).offset().top
                        }, 100, "linear", function () {
                            // when done, add hash to url
                            // (default click behaviour)
                            window.location.hash = hash;
                        });
                    });
                }
            }
        }

        smoothHashScroll();
        var toc = $('.ui-toc');
        var tocAffix = $('.ui-affix-toc');
        var tocDropdown = $('.ui-toc-dropdown');
        //toc
        tocDropdown.click(function (e) {
            e.stopPropagation();
        });

        var enoughForAffixToc = true;

        function generateScrollspy() {
            $(document.body).scrollspy({
                target: ''
            });
            $(document.body).scrollspy('refresh');
            if (enoughForAffixToc) {
                toc.hide();
                tocAffix.show();
            } else {
                tocAffix.hide();
                toc.show();
            }
            $(document.body).scroll();
        }

        function windowResize() {
            //toc right
            var paddingRight = parseFloat(markdown.css('padding-right'));
            var right = ($(window).width() - (markdown.offset().left + markdown.outerWidth() - paddingRight));
            toc.css('right', right + 'px');
            //affix toc left
            var newbool;
            var rightMargin = (markdown.parent().outerWidth() - markdown.outerWidth()) / 2;
            //for ipad or wider device
            if (rightMargin >= 133) {
                newbool = true;
                var affixLeftMargin = (tocAffix.outerWidth() - tocAffix.width()) / 2;
                var left = markdown.offset().left + markdown.outerWidth() - affixLeftMargin;
                tocAffix.css('left', left + 'px');
            } else {
                newbool = false;
            }
            if (newbool != enoughForAffixToc) {
                enoughForAffixToc = newbool;
                generateScrollspy();
            }
        }
        $(window).resize(function () {
            windowResize();
        });
        $(document).ready(function () {
            windowResize();
            generateScrollspy();
        });

        //remove hash
        function removeHash() {
            window.location.hash = '';
        }

        var backtotop = $('.back-to-top');
        var gotobottom = $('.go-to-bottom');

        backtotop.click(function (e) {
            e.preventDefault();
            e.stopPropagation();
            if (scrollToTop)
                scrollToTop();
            removeHash();
        });
        gotobottom.click(function (e) {
            e.preventDefault();
            e.stopPropagation();
            if (scrollToBottom)
                scrollToBottom();
            removeHash();
        });

        var toggle = $('.expand-toggle');
        var tocExpand = false;

        checkExpandToggle();
        toggle.click(function (e) {
            e.preventDefault();
            e.stopPropagation();
            tocExpand = !tocExpand;
            checkExpandToggle();
        })

        function checkExpandToggle () {
            var toc = $('.ui-toc-dropdown .toc');
            var toggle = $('.expand-toggle');
            if (!tocExpand) {
                toc.removeClass('expand');
                toggle.text('Expand all');
            } else {
                toc.addClass('expand');
                toggle.text('Collapse all');
            }
        }

        function scrollToTop() {
            $('body, html').stop(true, true).animate({
                scrollTop: 0
            }, 100, "linear");
        }

        function scrollToBottom() {
            $('body, html').stop(true, true).animate({
                scrollTop: $(document.body)[0].scrollHeight
            }, 100, "linear");
        }
    </script>
</body>

</html>
