任何人都可以帮助我理解以下代码
#include <iostream>
void foo(const char * c)
{
std::cout << "const char *" << std::endl;
}
template <size_t N>
void foo(const char (&t) [N])
{
std::cout << "array ref" << std::endl;
std::cout << sizeof(t) << std::endl;
}
int main()
{
const char t[34] = {'1'};
foo(t);
char d[34] = {'1'};
foo(d);
}
Run Code Online (Sandbox Code Playgroud)
输出是
const char *
array ref
34
Run Code Online (Sandbox Code Playgroud)
为什么第一个foo调用const char *版本?如何让它调用参考版本?
考虑以下C++代码,
template <typename Derived>
struct A
{
bool usable_;
};
template <typename Derived>
struct B : A< B<Derived> >
{
void foo()
{
usable_ = false;
}
};
struct C : B<C>
{
void foo()
{
usable_ = true;
}
};
int main()
{
C c;
}
Run Code Online (Sandbox Code Playgroud)
我有编译错误:在成员函数中void B<Derived>::foo():
template_inherit.cpp:12:错误:在此范围内未声明'useful_'.
这是为什么 ?任何好的修复?
当使用g ++编译以下代码时,我在'int''错误之前得到了'预期的primary-expression'.你知道为什么以及如何解决它吗?谢谢 !
struct A
{
template <typename T>
T bar() { T t; return t;}
};
struct B : A
{
};
template <typename T>
void foo(T & t)
{
t.bar<int>();
}
int main()
{
B b;
foo(b);
}
Run Code Online (Sandbox Code Playgroud)