小编xax*_*zak的帖子

pre-typedef一个variadic-function-pointer参数

我有一个函数foo,它使用可变参数函数指针作为其参数.

我想在函数声明之前使用"using"来定义参数的类型.

template <typename ... vARGS>
using TFuncType = void(*)(vARGS ... V_args);

template <typename ... vARGS>
void foo(TFuncType<vARGS ...> funcptr) {}

void bar(int i) {}

int main() {
  foo(&bar); // This line fails to compile.
}
Run Code Online (Sandbox Code Playgroud)

这不编译.错误(通过使用c ++ 1z的clang)是:

/make/proj/test/variadic-funcparam-deduce2.cpp:39:5: error: no matching function for call to 'foo'
foo(&bar);
^~~
/make/proj/test/variadic-funcparam-deduce2.cpp:33:36: note: candidate template ignored: substitution failure [with vARGS = int]
template <typename ... vARGS> void foo(TFuncType<vARGS ...> funcptr) {}
Run Code Online (Sandbox Code Playgroud)

为什么"int"替换失败了?

如果我在foo()中明确写出类型,我可以成功编译:

template <typename ... vARGS>
void foo(void(*funcptr)(vARGS …
Run Code Online (Sandbox Code Playgroud)

c++ templates function-pointers variadic c++11

15
推荐指数
1
解决办法
689
查看次数

C++:正确的模板参数的模板类型成员的语法?

我有一个采用模板类型参数(tTRAIT)的类.我想知道tTRAIT 的模板类型 成员 别名,但我无法弄清楚语法.(这有可能吗?).

template <bool bBOOL>
struct SFoo {};

struct STrait
    {
        template <bool bBOOL>
        using TFoo = SFoo<bBOOL>;
    };

template <typename tTRAIT>
struct SBar
    {
        template <bool bBOOL>
        friend typename tTRAIT::template TFoo<bBOOL>;
    };

SBar<STrait> bar;
Run Code Online (Sandbox Code Playgroud)

Clang的错误(friend在线)是:

error: friend type templates must use an elaborated type
Run Code Online (Sandbox Code Playgroud)

我试过用尽我能想到的所有可能的组合:

friend tTRAIT::TFoo;
friend tTRAIT::template TFoo;
friend typename tTRAIT::TFoo;
friend typename tTRAIT::template TFoo;
template <bool bBOOL> friend tTRAIT::TFoo;
template <bool bBOOL> friend tTRAIT::TFoo<bBOOL>;
template <bool bBOOL> friend tTRAIT::template TFoo; …
Run Code Online (Sandbox Code Playgroud)

c++ templates using friend c++11

10
推荐指数
1
解决办法
478
查看次数

Visual Studio Code:使用选项卡(而不是箭头键)选择智能感知建议?

就像在 ipython 中一样,在 VScode 中是否可以使用选项卡从智能感知中选择选项而不是使用箭头键?

intellisense keyboard-shortcuts autocomplete visual-studio-code

8
推荐指数
1
解决办法
2831
查看次数

"std::format"ing std::chrono 秒,不带小数位

在 c++20 中格式化秒(使用 std::format)而不显示小数位的正确方法是什么?


这段代码:

#include <chrono>
#include <format>
#include <iostream>

int main()
    {
        std::cout << std::format("{:%H:%M:%S}",  std::chrono::system_clock::now()) << std::endl;
        std::cout << std::format("{:%H:%M:%OS}", std::chrono::system_clock::now()) << std::endl;
    }
Run Code Online (Sandbox Code Playgroud)

产生此输出(通过主干 GCC (14.0.0 20230425)):

00:18:07.087240403
00:18:07.087622600
Run Code Online (Sandbox Code Playgroud)

我只想要整数部分(例如00:18:07没有尾随.087240403)。事后从结果字符串中删除剩余部分非常容易(或者手动提取秒数并单独打印它们),但如果这是预期的解决方案,我会感到非常惊讶。


此处描述了格式化代码。AFAICT 没有任何%S变体可以显式地将秒舍入或截断为最接近的整数。

%S(和下面的 %OS)的解释(来自上面的链接)是:

将秒写为十进制数。如果秒数小于 10,则结果以 0 为前缀。

如果输入的精度不能用秒精确表示,则格式是具有固定格式的十进制浮点数,其精度与输入的精度匹配(如果转换为浮点数,则为微秒精度)点小数秒不能在 18 个小数位以内)。小数点字符根据区域设置进行本地化。

修改后的命令 %OS 写入区域设置的替代表示形式。


GCC/glibc 在这里运行正常,对吗?(似乎符合上面的解释)。

如果是这样,实现这一目标的预期方法是什么?

  1. 我是否使用不同的格式字符串?
  2. 我使用不同的时钟吗?
  3. 我是否要先手动舍入该值?(如果四舍五入,它仍然显示小数部分)。
  4. 或者我真的必须手动删除小数部分或手动提取秒数。

如果这不是很容易实现,这是否可以被视为潜在的不良行为,也许是 STL 缺陷?)

c++ formatting stl c++-chrono c++20

6
推荐指数
1
解决办法
1102
查看次数

在成员结构的模板-模板参数中扩展外部结构的可变参数类型

如果您有模板结构,则可以使用 T 作为成员模板的值参数的类型:

\n
template <typename T>\nstruct SMoo\n    {\n        template <T I, typename T2>\n        struct SBaa {};\n    };\n\nSMoo<int>::SBaa<3, bool> moobaa;\n
Run Code Online (Sandbox Code Playgroud)\n

您可以对可变外部模板执行相同的操作:

\n
template <typename ... VT>\nstruct SMoo\n    {\n        template <VT ... VI, typename T2>\n        struct SBaa {};\n    };\n\nSMoo<int, bool>::SBaa<3, true, bool> moobaa;\n
Run Code Online (Sandbox Code Playgroud)\n

您还可以使用 T 作为成员模板的template-template 参数的 value 参数的类型:

\n
template <typename T>\nstruct SMoo\n    {\n        template <template <T I, typename T2> typename TT>\n        struct SBaa {};\n    };\n\ntemplate <int I, typename T>\nstruct SCaw {};\n\nSMoo<int>::SBaa<SCaw> moobaacaw;\n
Run Code Online (Sandbox Code Playgroud)\n …

c++ templates template-templates variadic-templates

5
推荐指数
0
解决办法
60
查看次数