我有一个函数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) 我有一个采用模板类型参数(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) 就像在 ipython 中一样,在 VScode 中是否可以使用选项卡从智能感知中选择选项而不是使用箭头键?
intellisense keyboard-shortcuts autocomplete visual-studio-code
在 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 在这里运行正常,对吗?(似乎符合上面的解释)。
如果是这样,实现这一目标的预期方法是什么?
(如果这不是很容易实现,这是否可以被视为潜在的不良行为,也许是 STL 缺陷?)
如果您有模板结构,则可以使用 T 作为成员模板的值参数的类型:
\ntemplate <typename T>\nstruct SMoo\n {\n template <T I, typename T2>\n struct SBaa {};\n };\n\nSMoo<int>::SBaa<3, bool> moobaa;\nRun Code Online (Sandbox Code Playgroud)\n您可以对可变外部模板执行相同的操作:
\ntemplate <typename ... VT>\nstruct SMoo\n {\n template <VT ... VI, typename T2>\n struct SBaa {};\n };\n\nSMoo<int, bool>::SBaa<3, true, bool> moobaa;\nRun Code Online (Sandbox Code Playgroud)\n您还可以使用 T 作为成员模板的template-template 参数的 value 参数的类型:
\ntemplate <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;\nRun Code Online (Sandbox Code Playgroud)\n … c++ ×4
templates ×3
c++11 ×2
autocomplete ×1
c++-chrono ×1
c++20 ×1
formatting ×1
friend ×1
intellisense ×1
stl ×1
using ×1
variadic ×1