我试图了解在哪里使用template,typename但遇到了一个我无法解决的问题。我有一个模板函数f<T>,它使用传递给它的类型(这将是一个类)来调用模板成员函数.f<T>。我认为我typename在函数体中的使用是正确的,但是,我不断收到以下错误:
source.cpp:在函数中
'void f()':
source.cpp:11:19: error: non-template'f'used as template
source.cpp:11:19: 注意:用于'typename T::C::template f'表示它是模板
struct A {
struct C {
template <typename T> void f() {}
};
};
template <typename T> void f() {
typename T::C::f<int>();
}
int main() {
f<A>();
}
Run Code Online (Sandbox Code Playgroud)
请注意它建议使用的最后一个错误'typename T::C::template f'。所以我做了以下改动:
// ...
typename T::C::template f<int>();
// ...
Run Code Online (Sandbox Code Playgroud)
我按照它说的做了,但后来我收到了下一行错误:
错误:没有名为类模板
'f'中'struct A::C'
我相信,这个错误是不正确的,有是在事实上称为公共模板函数f中struct …
为什么调用f不解析第一个函数重载?我收到错误:
source.cpp: In function 'int main()':
source.cpp:12:31: error: 'A' is an inaccessible base of 'B'
class A {}; class B : A {};
void f(const A &) { std::cout << "const A &"; }
template <typename T> void f(T) { std::cout << "Generic"; }
int main() {
B b;
f(dynamic_cast<const A &>(b));
}
Run Code Online (Sandbox Code Playgroud)
请注意,如果我取出dynamic_cast代码将工作,第二个 f被调用(它打印"通用").但我要做的就是第一次打电话.我认为它dynamic_cast会工作,但由于某种原因它会导致问题.我在这做错了什么?
我试图让它做到我不能从循环中调用线程.但是当我运行它时,我得到一个运行时错误:
terminate called after throwing an instance of 'std::system_error'
what(): Invalid argument Thread #1
#include <iostream>
#include <vector>
#include <memory>
#include <thread>
#include <mutex>
std::mutex m;
static int thread_count;
auto foo = [&] {
std::lock_guard<std::mutex> lock(m);
std::cout << "Thread #" << ++thread_count << std::endl;
};
int main()
{
std::vector<std::shared_ptr<std::thread>>
threads(20, std::make_shared<std::thread>(foo));
for (const auto& th : threads)
th->join();
}
Run Code Online (Sandbox Code Playgroud) 对于一个项目,我应该测量我正在使用的机器在C++中的基本原始操作的运行时间.它说:
写一个程序确定的(定时参数的值
fetch,store,+,-,*,/,<,函数调用,函数返回,new,delete,和[]),用于在其上运行的机器.
fetch并store是:
a = b + 1
Run Code Online (Sandbox Code Playgroud)
b和1是被"取出"(以及加入连同+),并存储在a.
我以前从未做过这样的事情.我需要使用时钟方法来计算运行时间吗?我的时间代码应该复杂还是简单?应该评估多少次?
我需要帮助理解关于加权快速联合的问题给出的解释:
以下哪个
id[]数组可能是对一组10项目运行加权快速联合算法的结果?检查所有适用。回想一下,我们的加权快速联合算法使用按大小(节点数)联合(而不是按高度联合)。
不正确:
9 1 7 3 4 9 6 7 8 9
解释:9-5 7-2 5-0不正确:
2 2 2 2 5 1 2 3 1 2
解释:2-9 3-7 9-3 5-4 0-2 1-8 8-4 4-9 8-6正确:
9 9 3 4 9 4 9 9 4 2
说明:id[]数组包含一个循环:2->3->4->9->2正确:
0 2 3 0 0 2 2 9 3 0
解释:以父为2 <根的树的大小是以父为根的树的大小的两倍2正确:
0 4 6 7 4 …
当我有:
std::ostringstream oss("Hello");
Run Code Online (Sandbox Code Playgroud)
为什么这样做:
std::cout << oss.str();
Run Code Online (Sandbox Code Playgroud)
但这不会打印任何内容:
std::cout << oss.rdbuf();
Run Code Online (Sandbox Code Playgroud)
阅读定义operator<<(std::ostream&, std::streambuf*)说它将从缓冲区打印字符。难道oss.rdbuf()没有包含什么吗?
如果我有一个lambda,它通过reference([&] {})捕获所有自动变量,为什么不能将它转换为函数指针?常规函数可以修改变量,就像lambda一样,可以通过引用捕获所有内容,为什么它不一样?
我想换句话说,lambda与&捕获列表和常规函数之间的功能差异是什么,使得lambda不能转换为函数指针?
为什么这段代码会出错而不是调用move-constructor?
struct A
{
};
int main()
{
A a{A()}; // error: too many initializers for 'A'
}
Run Code Online (Sandbox Code Playgroud)
演示:http://coliru.stacked-crooked.com/a/ce822fcbda0f7db9
我是否可以使用统一初始化调用聚合上的构造函数?
template从全局命名空间获取模板名称时,可以使用关键字:
template <class T> void function_template();
template <class T>
void h()
{
::template function_template<T>();
}
int main() { h<int>(); }
Run Code Online (Sandbox Code Playgroud)
但是这段代码没有它就可以编译.人们可能想要这样做的情况是什么?
这段代码是否格式良好?函数模板本身的声明在clang和gcc中都会出错,即使它很Ts可能是空的.
// error: too many template arguments for class template 'pair'
template<class I, class U, class... Ts>
void f(std::pair<I,U,Ts...>);
int main()
{
f(std::pair<int,int>());
}
Run Code Online (Sandbox Code Playgroud)
函数调用在gcc中给出了这个错误,这是没有意义的.没有转换为int:
note: cannot convert 'std::pair<int, int>()' (type 'std::pair<int, int>') to type 'int'
Run Code Online (Sandbox Code Playgroud)