是否有可能有一个成员变量,它能够从指向自身的指针计算指向包含对象的指针(在它的方法中)?
让我们在API中包含一个外部调用接口,如下所示:
template <typename Class, MethodId Id, typename Signature>
class MethodProxy;
template <typename Class, MethodId Id, typename ReturnT, typename Arg1T>
class MethodProxy<Class, Id, ReturnT ()(Arg1T) {
public:
ReturnT operator()(Class &invocant, Arg1T arg1);
};
Run Code Online (Sandbox Code Playgroud)
类似地,对于从0到N的其他数量的参数.对于外来的每个类,一个C++类声明具有一些特征,并且该模板使用这些特征(以及参数类型的更多特征)来查找和调用外部方法.这可以像:
Foo foo;
MethodProxy<Foo, barId, void ()(int)> bar;
bar(foo, 5);
Run Code Online (Sandbox Code Playgroud)
现在我想做的是以Foo
这种方式定义,我可以这样称呼:
Foo foo;
foo.bar(5);
Run Code Online (Sandbox Code Playgroud)
不重复签名多次.(显然创建一个静态成员并在方法中包装调用很简单,对吧).嗯,事实上,这仍然很容易:
template <typename Class, MethodId Id, typename Signature>
class MethodMember;
template <typename Class, MethodId Id, typename ReturnT, typename Arg1T>
class MethodMember<Class, Id, ReturnT ()(Arg1T) {
MethodProxy<Class, Id, Signature> method;
Class …
Run Code Online (Sandbox Code Playgroud) 如何计算大n
和和的二项式系数模数142857 r
.142857有什么特别之处吗?如果问题是模数p
在哪里p
是素数那么我们可以使用卢卡斯定理但是应该为142857做什么.
我正在追踪 gcc 中的一个错误,并遇到了一种我不知道可接受的行为是什么的情况。假设我们在协程中的某处有一行,例如:
Pair{.x{}, .y=(co_await std::suspend_always{}, 1)};
Run Code Online (Sandbox Code Playgroud)
其中Pair
是一些可以使用成员.x
(X
具有默认构造函数的类型)和(具有 int 构造函数的.y
类型)进行聚合初始化的结构。Y
这些类型可能都有重要的析构函数。这段代码可以做什么?
C++20 标准第 9.4.1/6 节指出:
聚合元素的初始化按元素顺序进行评估。也就是说,与给定元素相关的所有值计算和副作用都按顺序排列在其后面的任何元素的计算和副作用之前。
这向我表明该行唯一正确的行为如下:
Pair
。.x
通过调用初始化成员X::X()
co_await
表达式(调用await_ready
和await_suspend
);暂停。然后,如果协程恢复,我期望:
await_resume
服务员。.y
通过调用初始化成员Y::Y(int)
Pair::~Pair()
临时销毁。如果协程被破坏,我预计:
X::~X()
给.x
会员。我相当确定这是正确的行为,尽管不太确定这是否是唯一允许的行为 - 而且我的困惑由于没有一个主要编译器针对这种情况输出合理的代码而变得更加严重。godbolt 上的完整示例表明,gcc 和 clang 生成的代码中对析构函数和构造函数的调用不正确匹配(这是我首先在 gcc 中寻找的错误)。MSVC 给出内部编译器错误。
建议的行为正确吗?这是唯一正确的行为吗?
在这里,我尝试用C++编写程序来查找NCR.但是我在结果中遇到了问题.这是不正确的.你能帮我找一下程序中的错误吗?
#include <iostream>
using namespace std;
int fact(int n){
if(n==0) return 1;
if (n>0) return n*fact(n-1);
};
int NCR(int n,int r){
if(n==r) return 1;
if (r==0&&n!=0) return 1;
else return (n*fact(n-1))/fact(n-1)*fact(n-r);
};
int main(){
int n; //cout<<"Enter A Digit for n";
cin>>n;
int r;
//cout<<"Enter A Digit for r";
cin>>r;
int result=NCR(n,r);
cout<<result;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 以下std::array <char, N>
使用字符串文字在构造函数中初始化成员的示例不能在GCC 4.8上编译,而是使用Clang 3.4进行编译.
#include <iostream>
#include <array>
struct A {
std::array<char, 4> x;
A(std::array<char, 4> arr) : x(arr) {}
};
int main() {
// works with Clang 3.4, error in GCC 4.8.
// It should be the equivalent of "A a ({'b','u','g','\0'});"
A a ({"bug"});
for (std::size_t i = 0; i < a.x.size(); ++i)
std::cout << a.x[i] << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在第一印象,它看起来像一个GCC错误.我觉得它应该编译,因为我们可以std::array<char, N>
直接用字符串文字初始化.例如:
std::array<char, 4> test = {"bug"}; //works
Run Code Online (Sandbox Code Playgroud)
我很想知道标准对此有何看法.
在下面的代码中,我正在尝试构建一个类型的网格.例如,在float
和之间int
,将结果推广到float
:
float join(float f, int) { return f; }
float join(float f, float) { return f; }
Run Code Online (Sandbox Code Playgroud)
然后我介绍一种wrapper
类型:
template <typename Inner>
struct wrapper
{
using inner_t = Inner;
inner_t value;
};
Run Code Online (Sandbox Code Playgroud)
其join
操作行为很自然:
template <typename Inner1, typename Inner2>
auto
join(const wrapper<Inner1>& w1, const wrapper<Inner2>& w2)
-> wrapper<decltype(join(w1.value, w2.value))>
{
return {join(w1.value, w2.value)};
}
Run Code Online (Sandbox Code Playgroud)
它也可以join
用"标量"类型编辑:
template <typename Inner1, typename T2>
auto
join(const wrapper<Inner1>& w1, const T2& value2)
-> wrapper<decltype(join(w1.value, value2))> …
Run Code Online (Sandbox Code Playgroud) 我有一个相当奇怪的问题,告诉googletest以我想要的方式使用PrintTo打印某个类.
该类是一个非常简单的2D点,它位于命名空间中,PrintTo函数位于同一名称空间中.事实上,我有一个完美打印的派生类(3D点).
以下是测试和PrintTo函数的一些代码(命名空间名称编辑,但其他所有内容都是从实际代码中复制和粘贴的):
// PrintTo Functions
namespace MyNamespace
{
void PrintTo(const MyNamespace::CPunto2D& pto, ::std::ostream* os)
{
*os << "(";
*os << pto.X();
*os << ",";
*os << pto.Y();
*os << ")";
}
void PrintTo(const MyNamespace::CPunto3D& pto, ::std::ostream* os)
{
*os << "(";
*os << pto.X();
*os << ",";
*os << pto.Y();
*os << ",";
*os << pto.m_Z;
*os << ")";
}
}
// Tests
TEST(TestPrintTo, TestPunto2D)
{
MyNamespace::CPunto2D p1(1,1);
MyNamespace::CPunto2D pRef(5,6);
ASSERT_THAT(p1, Eq(pRef));
}
TEST(TestPrintTo, TestPunto3D)
{
MyNamespace::CPunto3D pCentro(1,1,1);
MyNamespace::CPunto3D …
Run Code Online (Sandbox Code Playgroud) 给出以下C接口:
IoT_Error_t aws_iot_mqtt_subscribe(AWS_IoT_Client *pClient,
const char *pTopicName,
uint16_t topicNameLen,
QoS qos,
pApplicationHandler_t pApplicationHandler,
oid *pApplicationHandlerData);
Run Code Online (Sandbox Code Playgroud)
" aws_iot_mqtt_subscribe
存储其后来引用的参数 - 在稍后的某个时间点响应某些事件来调用"
处理器:
typedef void (*pApplicationHandler_t)(
AWS_IoT_Client *pClient,
char *pTopicName,
uint16_t topicNameLen,
IoT_Publish_Message_Params *pParams,
void *pClientData);
Run Code Online (Sandbox Code Playgroud)
我试图将其包装到具有以下接口的C++类中:
class AWS {
// ...
public:
void subscribe(const std::string &topic,
std::function<void(const std::string&)> callback);
// ...
};
Run Code Online (Sandbox Code Playgroud)
我的目标是使捕获lambda函数成为可能AWS::subscribe
.我已经尝试了近一个星期的不同方法,但似乎没有一个工作.
如果有任何其他需要了解问题,请告诉我,我很高兴更新问题.
以下代码是否形成良好?
class B;
template<class T>
class A
{
B do_f() const;
friend auto f(A const& a) {return a.do_f();} // #1
};
class B{};
template <class T>
B A<T>::do_f() const { return B{};}
int main()
{
A<double> a;
f(a);
}
Run Code Online (Sandbox Code Playgroud)
如果我改变auto
了#1 B
,我得到了不完整的类型错误消息.
编译auto
为gcc/clang 演示
演示失败B
我尝试创建无法与原始别名区分开的模板别名。
因此,我创建特征以检查2个模板(非类型)是否相等:
template <template <class...> class C1,
template <class...> class C2>
struct is_same_template : std::false_type {};
template <template <class...> class C1>
struct is_same_template<C1, C1> : std::true_type {};
Run Code Online (Sandbox Code Playgroud)
现在测试一下:
// Expected alias
template <typename ... Ts> using V_Ts = std::vector<Ts...>; // Variadic
// Fallback alias
template <typename T, typename A> using V = std::vector<T, A>; // Exact count
static_assert(!is_same_template<std::vector, V_Ts>::value); // Alias rejected by gcc/clang
static_assert( is_same_template<std::vector, V>::value); // Alias accepted only for gcc
Run Code Online (Sandbox Code Playgroud)
是否可以创建“ true”别名?哪个编译器是正确的?
c++ ×9
c++11 ×2
templates ×2
algorithm ×1
auto ×1
c++20 ×1
compiler-bug ×1
decltype ×1
gcc ×1
googletest ×1
math ×1
offsetof ×1
overloading ×1
properties ×1
stdarray ×1
wrapper ×1