我遇到auto和decltype问题.
void f(const vector<int>& a, vector<float>& b)
{
typedef decltype(a[0]*b[0]) Tmp;
for (int i=0; i < b.size(); ++i) {
auto p0 = new auto(a[i]*b[i]);
auto p1 = new decltype(a[i]*b[i]);
*p0=a[i]*b[i];
*p1=a[i]*b[i];
cout<<*p0<<endl;
cout<<*p1<<endl;
delete p0;
delete p1;
}
}
int main()
{
vector<float>vec2;
vec2.push_back(2.0);
vector<int>vec1;
vec1.push_back(5);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码在GCC4.7中运行良好.我可以使用'new auto(a [0]*b [0])'为[0]*b [0]的类型分配内存吗?在这种情况下,我无法区分decltype和auto之间的区别.
std::shared_ptr<int> g_s = std::make_shared<int>(1);
void f1()
{
std::shared_ptr<int>l_s1 = g_s; // read g_s
}
void f2()
{
std::shared_ptr<int> l_s2 = std::make_shared<int>(3);
std::thread th(f1);
th.detach();
g_s = l_s2; // write g_s
}
Run Code Online (Sandbox Code Playgroud)
关于上面的代码,我知道不同的线程读取和写入相同的shared_ptr导致竞争条件.但是怎么样weak_ptr?下面的代码中是否有竞争条件?(我的平台是Microsoft VS2013.)
std::weak_ptr<int> g_w;
void f3()
{
std::shared_ptr<int>l_s3 = g_w.lock(); //2. here will read g_w
if (l_s3)
{
;/.....
}
}
void f4()
{
std::shared_ptr<int> p_s = std::make_shared<int>(1);
g_w = p_s;
std::thread th(f3);
th.detach();
// 1. p_s destory will motify g_w (write g_w)
}
Run Code Online (Sandbox Code Playgroud) #include <string>
struct String
{
template<typename T> operator T*() { return 0; }
operator std::string() { return ""; }
};
int main()
{
String myStr;
std::string str1(myStr); // ambiguous, error C2668
std::string str2 = myStr; // error C2440:
// 'initializing' : cannot convert from 'String' to
// `std::basic_string<char,std::char_traits<char>,std::allocator<char>>',
// No constructor could take the source type,
// or constructor overload resolution was ambiguous
const std::string& rStr = myStr; // Ok, but why?
}
Run Code Online (Sandbox Code Playgroud)
我正在使用VS 2013.
问题:
为什么定义str1和str2 …
typedef void(&&RF)(void* p);
RF rf()
{
return f;
}
int ay[10] = { 0 };
typedef int(&&RA)[10];
RA ra()
{
return ay; // error
}
cout << is_lvalue_reference<decltype(rf())>::value << endl; // 1
Run Code Online (Sandbox Code Playgroud)
C++参考文献说"对函数的rvalue引用被视为左值,无论是否命名".
但我无法理解这是什么考虑因素?我想也许函数的名称总是左值.所以它必须保持左值的属性,并确保将函数名称传递给可以调用的任何地方,比如rf()(NULL).然后数组名称在我脑海中被禁止.我认为它也总是一个左值,所以我编写了上面的代码来测试它并得到一个错误.
谁能指出所有这一切背后的真正原因?
struct mystruct
{
int i;
double f;
} ;
typedef mystruct myotherstruct;
//the other .cpp file
struct mystruct; //OK,this is a correct forward declaration.
struct myotherstruct; // error C2371?in vc2k8?: 'myotherstruct' : redefinition; different basic types
Run Code Online (Sandbox Code Playgroud)
大家好.为什么我不能转发声明myotherstruct?
#include <stdio.h>
constexpr size_t constLength(const char* str)
{
return (*str == 0) ? 0 : constLength(str + 1) + 1;
}
int _tmain(int argc, _TCHAR* argv[])
{
const char* p = "1234567";
size_t i = constLength(p);
printf(p);
printf("%d", i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
嗨,我想在compile-time中得到一个字符串的长度.所以我写了上面的代码.但是在反汇编代码中,我发现下面名为sub_401000的'constLength'函数将导致计算字符串长度的运行时开销.有什么问题吗?(Visual Studio 2015 Preview,最大化速度(/ O2)优化发布)
int __cdecl sub_401010()
{
int v0; // esi@1
v0 = sub_401000("234567") + 1;
sub_401040(&unk_402130);
sub_401040("%d");
return 0;
}
int __thiscall sub_401000(void *this)
{
int result; // eax@2
if ( *(_BYTE *)this )
result …Run Code Online (Sandbox Code Playgroud) struct A
{
int i;
};
struct B
{
B(){}
int i;
}
Run Code Online (Sandbox Code Playgroud)
大家好
我知道"POD意味着普通旧数据类型,根据定义,它不能具有用户定义的构造函数".但是,当"定义具有用户定义的默认值"时,我无法理解为什么此规则有效.
它们的内存布局没有区别.为什么A类是POD,而不是B?
据我所知,可能的实现std::result_of是
template<class F, class... ArgTypes>
struct result_of<F(ArgTypes...)>
{
typedef decltype(
std::declval<F>()(std::declval<ArgTypes>()...)
) type;
};
Run Code Online (Sandbox Code Playgroud)
但是当我使用时我std::result_of遇到了一些麻烦.
int f(int x)
{
return 0;
}
template<typename T>
void Test(const T& t)
{
decltype(std::declval<T>()(std::declval<int>())) i1 = 1; // ok
typename std::result_of<T(int)>::type i2 = 2; // compile error:
// function returning a function
// I think this means the same thing just as above, right?
}
int main()
{
Test(f);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这两种形式有什么区别?
c++ ×8
c++11 ×7
auto ×1
constexpr ×1
constructor ×1
declaration ×1
decltype ×1
pod ×1
type-traits ×1
typedef ×1
weak-ptr ×1