#include <iostream>
struct A
{
void update(bool const & v)
{
std::cout << std::boolalpha << v << std::endl;
}
void update(std::string_view v)
{
std::cout << v << std::endl;
}
};
template <typename T>
void update(T const & item)
{
A a;
a.update(item);
}
int main()
{
const char * i = "string";
update(i);
}
Run Code Online (Sandbox Code Playgroud)
当我用a调用update时const char *,编译器使用bool参数而不是string_view?!调用函数。为什么??
两个模板参数为同一类型如何部分专业化。
如何使用第二个函数编写此代码。
#include <utility>
#include <iostream>
template <typename A, typename B>
void Translate(A&& a,B* b){
// make some translate from a to b
// b->bvalue=a.av;
std::cout<<"normal function";
}
//if a and b are same type,
template <typename A>
void Translate(A&& a, A* b) {
*b = std::forward<A>(a);
std::cout<<"forward function";
}
int main(int argc, char** argv) {
int in=0,out=0;
Translate(in,&out);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
期望输出“转发功能”。
c++ templates partial-specialization specialization overload-resolution
我试图弄清楚为什么您为std::sort和定义了不同的自定义比较功能的原因std::priority_queue。
例如,因为std::sort我可以做这样的事情:
bool compare(const vector<int>& a, const vector<int>& b)
{
return a[0] < b[0];
}
class foo
{
public:
vector<vector<int>> f(vector<vector<int>> list)
{
std::sort(list.begin(), list.end(), compare);
return list;
}
};
int main()
{
vector<vector<int>> t = { {2,1},{1,0},{3,7} };
foo n;
auto ans = n.f(t);
for (vector<int> x : ans)
{
printf("x[0]: %d , x[1]: %d \n", x[0], x[1]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
运行代码后,结果为:
x [0]:1,x [1]:0
x [0]:2,x [1]:1
x [0]:3,x [1]:7
但是,如果我这样定义另一个函数 …
考虑以下代码。在这里,如果我们std::begin对initializer_list显式使用未命名std::,则可以正常工作。如果我们省略了std::并begin在named上使用initializer_list,它也可以正常工作。但是,如果我们std::与第一种情况类似并省略其余部分,则它将无法编译。
#include <iostream>
#include <iterator>
void func(int len, const int* x)
{
for(int i=0;i<len;++i)
std::cout << x[i] << "\n";
}
int main()
{
{
// OK
func(5, std::begin({1,3,6,823,-35}));
}
{
// OK
auto&& list = {1,3,6,823,-35};
func(5, begin(list));
}
// {
// // Fails to compile
// func(5, begin({1,3,6,823,-35}));
// }
}
Run Code Online (Sandbox Code Playgroud)
我收到以下编译错误(在取消注释错误代码后):
test.cpp: In function ‘int main()’:
test.cpp:21:11: error: ‘begin’ was not declared in this …Run Code Online (Sandbox Code Playgroud) 如何专门化在基类中定义为纯函数的模板化函数?
struct A {
virtual void func(int a) = 0;
//virtual void func(int a) {} // replace above line with this and it works
};
struct B : public A {
template<typename T> void func(T t) {
cout <<"hello"<<endl;
}
};
template<> void B::func<int>(int a) { cout <<"hello 2"<<endl; }
int main() {
B b;
b.func(2);
}
Run Code Online (Sandbox Code Playgroud)
错误:
错误:变量类型“ B”是抽象类B b;^注意:'B'中的未实现的纯虚拟方法'func'虚拟void func(int a)= 0;
我有以下代码:
#include <string_view>
class Foo
{
public:
Foo(std::string_view) {}
};
Run Code Online (Sandbox Code Playgroud)
当我这样做时,一切都可以正常编译(使用clang v8,C ++ 17):
Foo f("testing");
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用副本初始化,它将失败:
Foo f = "testing";
Run Code Online (Sandbox Code Playgroud)
诊断:
prog.cc:15:9: error: no viable conversion from 'const char [8]' to 'Foo'
Foo f = "testing";
^ ~~~~~~~~~
prog.cc:7:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'const char [8]' to 'const Foo &' for 1st argument
class Foo
^
prog.cc:7:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from …Run Code Online (Sandbox Code Playgroud) 可以说我有代码:
main.cpp:
my_obj1 obj1("hello obj1");
my_obj2 obj2("hello obj2");
int main()
{
:
:
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否obj1总是被保证创建之前obj2。
如果我认为这两个对象在课堂上都可以,那将是正确的。
假设你有一个这样的函数:
void func(int & arg) {
std::cout << arg << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
正在做类似的事情:
int x = 10;
int & y = x;
func(y);
Run Code Online (Sandbox Code Playgroud)
等同于:
int x = 10;
func(x)
Run Code Online (Sandbox Code Playgroud)
是否arg成为int && arg或是否留int & arg,当你像第一种情况下预先声明引用传递?
MSVC 2019 允许我定义这样的 lambda,其中模板参数未在参数列表中使用:
auto foo = []<bool B>() {
return B;
};
Run Code Online (Sandbox Code Playgroud)
但是,当尝试像这样调用它时,它会给出语法错误?
foo<false>();
Run Code Online (Sandbox Code Playgroud)
使用非参数模板参数调用 lambda 的正确方法是什么?
有什么方法可以std::ostream operator<<在其他函数调用中传递 an 作为参数吗?例如:
\n#include <iostream>\n\ntemplate <typename Visitor>\nvoid print(Visitor v, int value)\n{\n v(value);\n}\n\nint main(void)\n{\n\n std::cout.operator<<(5); // This works\n\n std::cout << 5; // This works\n\n print(std::cout.operator<<, 5); // Error happens\n\n return 0;\n}\n\nRun Code Online (Sandbox Code Playgroud)\n错误是:
\n$ g++ -g main.cpp -o main\nmain.cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\nmain.cpp:17:12: error: no matching function for call to \xe2\x80\x98print(<unresolved overloaded function type>, int)\xe2\x80\x99\n 5);\n ^\nmain.cpp:4:6: note: candidate: template<class Visitor> void print(Visitor, int)\n void print(Visitor v, int value)\n ^~~~~\nmain.cpp:4:6: note: template argument deduction/substitution failed:\nmain.cpp:17:12: note: …Run Code Online (Sandbox Code Playgroud)