如何引用作为模板参数给出的类的别名模板到从模板基类继承的A类?CB
#include <vector>
struct A
{
// the alias template I want to refer to:
template<class T>
using Container = std::vector<T>;
};
// the base class
template<template<class> class _Container>
struct B
{
_Container<int> m_container;
};
template<class _A>
struct C : public B< typename _A::Container >
{// ^^^^^^^^^^^^^^^^^^^^^^
};
int main()
{
C<A> foo;
}
Run Code Online (Sandbox Code Playgroud)
我尝试了几种解决方案,通过template在语句中的每个可能的位置添加关键字(例如template<class T> typename _A::Container<T>,typename _A::template Container...),但g++给出“模板参数 1 无效”或“类型/值不匹配”!
我知道这个网站上有很多类似的问题。我真的很喜欢以下链接中提到的解决方案:
\n\n\n\n经过一些修改,您可以在编译时包含文本文件,例如:
\n\nconstexpr const char* s = \n#include "file.txt"\nRun Code Online (Sandbox Code Playgroud)\n\n但是要使其工作,您必须向原始文件添加字符串文字前缀和后缀,例如
\n\nR"(\nThis is the original content,\nand I don\'t want this file to be modified. but i\n don\'t know how to do it.\n)";\nRun Code Online (Sandbox Code Playgroud)\n\n我的问题是:有没有一种方法可以使其工作但不修改 file.txt?
\n\n(我知道我可以使用命令行工具来制作副本,在副本前面添加和附加到副本,在编译后删除副本。我正在寻找比这更优雅的解决方案。希望不需要其他工具)
\n\n这是我尝试过的(但不起作用):
\n\n#include <iostream>\n\nint main() {\n constexpr const char* s =\n#include "bra.txt" // R"(\n#include "file.txt" //original file without R"( and )";\n#include "ket.txt" // )";\n std::cout << s << "\\n";\n return 0;\n}\n\n/opt/gcc8/bin/g++ -std=c++1z a.cpp\nIn file included from a.cpp:5:\nbra.txt:1:1: …Run Code Online (Sandbox Code Playgroud) 遗憾的是,我无法使用 C++ 中的任何stl / std库,因为我正在为嵌入式操作系统进行编程,该操作系统仅具有可用的gcc和 4.4.4裸 C++,因此,没有std::tuple、std::forward或。std::applystd::anything_else
为了帮助理解元通用生成的代码,我提供了一个用clang编译的最小示例代码,因为它可以选择向我们显示生成的模板元编程/元编程代码。
这个问题只是出于好奇,因为我可以按照正确的顺序创建它,而不是以错误的顺序生成整数参数包。这是我用来以错误的顺序生成整数打包程序包的方法:
template<int ...>
struct MetaSequenceOfIntegers { };
template<int AccumulatedSize, typename Tn, int... GeneratedSequence>
struct GeneratorOfIntegerSequence;
template<int AccumulatedSize, typename Grouper, typename Head, typename... Tail, int... GeneratedSequence>
struct GeneratorOfIntegerSequence< AccumulatedSize, Grouper( Head, Tail... ), GeneratedSequence... >
{
typedef typename GeneratorOfIntegerSequence
< AccumulatedSize + sizeof(Head), Grouper( Tail... ), AccumulatedSize, GeneratedSequence...
>::type type;
};
template<int AccumulatedSize, typename Grouper, int... GeneratedSequence> …Run Code Online (Sandbox Code Playgroud) c++ metaprogramming template-meta-programming variadic-templates c++11
对于我的工作,我必须编写 600 行以上的脚本。我的脚本对我的同事来说是可读的,这一点很重要,但我注意到,即使在阅读我自己的代码时,我也需要滚动很多次才能找到某个函数。
问题不在于代码丑陋或没有注释——每一行都有注释解释正在做什么以及为什么。它也是有组织的——它遵循一个非常可预测的结构。我所困扰的是所有垂直滚动。这似乎已经过时了。当我编写单词或乳胶文档时,我可以创建被软件识别为索引点的标题。这些索引点随后显示在目录中。
我可以在 R 中做类似的事情吗?有没有任何编程语言允许像这样的索引点?维基百科也这样做,例如链接https://en.wikipedia.org/wiki/Meteorological_history_of_Hurricane_Patricia#Peak_strength将带您进入有关飓风峰值强度的段落。
我想委托__iter__可迭代容器的方法。
class DelegateIterator:
def __init__(self, container):
attribute = "__iter__"
method = getattr(container, attribute)
setattr(self, attribute, method)
d = DelegateIterator([1,2,3])
for i in d.__iter__(): # this is successful
print(i)
for i in d: # raise error
print(i)
Run Code Online (Sandbox Code Playgroud)
输出是
1
2
3
Traceback (most recent call last):
File "test.py", line 10, in <module>
for i in d:
TypeError: 'DelegateIterator' object is not iterable
Run Code Online (Sandbox Code Playgroud)
请让我知道如何委托__iter__方法。
的{rlang}文档help("nse-force")给出了以下内容:
函数参数的卷曲运算符 {{ }} 有点特殊,因为它强制函数参数并立即解除它。解融合的表达式被就地替换,准备在另一个上下文(例如数据帧)中进行计算。
我同样对 'bang-bang' 运算符感到困惑!!,关于它的文档对于幕后发生的事情同样迟钝。
我的问题不是关于如何使用该运算符,因为它的用法(我认为)非常简单。相反,我想知道这样的运算符在{rlang}幕后实际上是如何实现的。根据该包的作者之一的说法,{{ foo }}基本上变成了!!rlang::enquo(foo). 然而,我仍然不知道像这样的非标准运算符实际上是如何实现的,特别是考虑到这个运算符似乎“正常工作”,无论它是否被实际上它只适用于由 {rlang} 支持的函数 - 感谢@Konrad Rudolph 的更正。{rlang}函数使用。
查看源代码,我只能猜测它是用 C 或 C++ 完成的。谁能给我更多信息吗?
我正在开发一个C++库,有两个函数:func1和func2。
func1如果开发人员在调用之前忘记调用,我想生成编译时错误func2:
这样就可以了:
func1();
func2(); // OK
Run Code Online (Sandbox Code Playgroud)
但这会失败:
func2(); // ERROR, you forget to call func1()
Run Code Online (Sandbox Code Playgroud)
当然,很容易生成运行时错误,但我想生成编译时错误。
我已经尝试如下,但它不起作用,因为我无法修改constexpr变量:
static constexpr bool b {false};
void func1() {
b = true; // ERROR!
}
typename<std::enable_if_t<b == true>* = nullptr>
void func2() {}
Run Code Online (Sandbox Code Playgroud)
我不太擅长元编程。我想知道在这种情况下是否可能生成编译时错误。
我想编写一个调试宏,在出现错误时打印调用的函数的名称。问题是有些函数返回值,我想从宏返回任何值。
\n这是我的尝试:
\n#define check(expr) _check_expr(#expr, expr)\n\nextern bool check_for_error();\n\ntemplate <typename T> inline T _check_expr(const char *str, T value)\n{\n if (check_for_error()) {\n fprintf(stderr, "Failure: %s\\n", str);\n }\n\n return value;\n}\nRun Code Online (Sandbox Code Playgroud)\n我遇到的问题是,有时T = void编译器不会让我将类型表达式传递void给函数:
../src/render.cc: In constructor \xe2\x80\x98render::impl::impl()\xe2\x80\x99:\n../src/render.cc:34:20: error: invalid use of void expression\n 34 | check(glDisable(GL_DEPTH_TEST));\nRun Code Online (Sandbox Code Playgroud)\n我无法重新定义在宏下调用的函数check或check_for_error函数,这些函数是我的程序外部的。此外,在计算表达式之后需要检查错误。
C++中有没有好的方法来解决这个问题?
\n类似于:“如果该表达式的 decltype 为 void,则生成此代码,否则生成该代码”。
\n在红宝石中:
module Example
class ExampleValidator
def mymethod
# do something important
end
end
end
Run Code Online (Sandbox Code Playgroud)
是否可以使其mymethod不可变(即没有猴子修补)?
不然“冻结”全班也可以接受ExampleValidator。
所以我最近接触到了C++中所谓的“令人厌恶的函数类型”的怪诞(据我所知源自这篇论文:https ://www.open-std.org/jtc1/sc22/wg21/文档/论文/2015/p0172r0.html)。我想了一会儿,它似乎确实有一定的道理,但如果有人以某种方式将它们从语言中完全删除,一切都会变得更加干净和更令人满意。
也许还有其他来源,但是(至少对我来说)令人讨厌的函数类型的大多数问题都来自于处理成员函数。如果我想获取成员函数的类型,我会这样做:
template <typename T>
struct remove_ptr_to_member { using type = T; };
template <typename T, typename class_t>
struct remove_ptr_to_member<T class_t::*> { using type = T; };
struct container {
void func() const;
};
using member_function_type = typename remove_ptr_to_member<decltype(container::func)>::type;
Run Code Online (Sandbox Code Playgroud)
const由于的声明中存在 a func(),删除指向成员的指针部分会给我们留下一个令人讨厌的函数。
this如果将不可见参数放入成员函数的类型中,则可以避免这种情况。然后,通常会导致令人讨厌的函数的东西都将应用于参数this(const意味着一个const this指针,&&意味着对 实例的右值引用this,等等......)。
现在,即使删除类型的指向成员的指针部分,剩下的也只是完全正常的函数类型。这样做的好处是,它会减少您在实现诸如is_function.
经典方法:
// primary template
template<class>
struct is_function : std::false_type { };
// specialization for regular functions …Run Code Online (Sandbox Code Playgroud)