用户定义的文字可以将函数作为参数吗?

发布于 2024-12-15 05:35:35 字数 273 浏览 0 评论 0原文

函数可以与用户定义的文字一起使用吗?

如果可以,可以做什么?这合法吗?

void operator "" _bar(int (*func)(int)) {
  func(1);
}

int foo(int x) {
  std::cout << x << std::endl;
}

int main() {
  foo(0);    // print 0
  foo_bar;   // print 1
}

Can functions be used with user defined literals?

If so, what shenanigans can be done? Is this legal?

void operator "" _bar(int (*func)(int)) {
  func(1);
}

int foo(int x) {
  std::cout << x << std::endl;
}

int main() {
  foo(0);    // print 0
  foo_bar;   // print 1
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

听风念你 2024-12-22 05:35:35

根据 C++11 Feb 2011 Draft § 2.14.8,用户文字类型为整型文字、浮点文字、字符串文字和字符文字。没有办法实现函数字面量类型。

用户定义的文字被视为对文字运算符的调用或
文字运算符模板 (13.5.8)。确定本次调用的形式
对于给定的带有 ud 后缀 X 的用户定义文字 L,
查找文字后缀标识符为 X 的literal-operator-id
在 L 的上下文中使用非限定名称查找规则
(3.4.1)。令 S 为本次查找找到的声明集。 S
不得为空。

整数:

operator "" X (n ULL)
operator "" X ("n")
operator "" X <’c1’, ’c2’, ... ’ck’>()

浮点数:

operator "" X (f L)
operator "" X ("f")
operator "" X <’c1’, ’c2’, ... ’ck’>()

字符串:

operator "" X (str, len)
operator "" X <’c1’, ’c2’, ... ’ck’>() //unoffcial, a rumored GCC extension

字符:

operator "" X (ch)

According to the C++11 Feb 2011 Draft § 2.14.8, the user literal types are integer-literals, floating-literals, string-literals, and character-literals. There is no way to do a function-literal type.

A user-defined-literal is treated as a call to a literal operator or
literal operator template (13.5.8). To determine the form of this call
for a given user-defined-literal L with ud-suffix X, the
literal-operator-id whose literal suffix identifier is X is looked up
in the context of L using the rules for unqualified name lookup
(3.4.1). Let S be the set of declarations found by this lookup. S
shall not be empty.

Integers:

operator "" X (n ULL)
operator "" X ("n")
operator "" X <’c1’, ’c2’, ... ’ck’>()

Floating:

operator "" X (f L)
operator "" X ("f")
operator "" X <’c1’, ’c2’, ... ’ck’>()

String:

operator "" X (str, len)
operator "" X <’c1’, ’c2’, ... ’ck’>() //unoffcial, a rumored GCC extension

Character:

operator "" X (ch)
冰之心 2024-12-22 05:35:35

看看foo_bar,它只是一个词汇标记。它被解释为名为 foo_bar 的单个标识符,而不是带有 _bar 后缀的 foo

Look at foo_bar, its just a single lexical token. Its interpreted as a single identifier named foo_bar, not as foo suffixed with _bar.

夜灵血窟げ 2024-12-22 05:35:35

不会

。C++ 有意避免这种恶作剧,因为如果符号 foo_bar 在您的示例中使用之前没有立即定义,那么它会很难理解。

您可以使用预处理器实现类似的效果。

#define bar (1)

int foo(int x) {
  std::cout << x << std::endl;
}

int main() {
  foo(0);    // print 0
  foo bar;   // print 1
}

No.

C++ intentionally avoids such shenanigans since the symbol foo_bar would be very difficult to comprehend if it weren't defined immediately before its use in your example.

You could achieve something similar with the preprocessor.

#define bar (1)

int foo(int x) {
  std::cout << x << std::endl;
}

int main() {
  foo(0);    // print 0
  foo bar;   // print 1
}
凉世弥音 2024-12-22 05:35:35

我不知道这是否会增加任何内容,但没有什么可以阻止您定义

PythonScript operator"" _python(const char*, std::size_t len) {...}

R"Py(
  print "Hello, World"
)Py"_python;

我实际上认为用户定义的文字将成为嵌入脚本或 SQL 的好方法。

I don't know if this adds anything but there's nothing preventing you from defining

PythonScript operator"" _python(const char*, std::size_t len) {...}

R"Py(
  print "Hello, World"
)Py"_python;

I actually think user-defined literals would make a nice way to embed scripts or SQL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文