我正在使用GCC v4.6获得一个-Wunused-set-variable警告,代码如下:
for ( auto i : f.vertexIndices ) {
Sy_MatrixFuzzyHashable< Vector3f > wrapper( cp );
if ( !vMapper.contains( wrapper ) ) {
mesh.vertexNormals() << cp;
i.normal = mesh.vertexNormals().size() - 1;
} else {
i.normal = vMapper.value( wrapper );
}
}
Run Code Online (Sandbox Code Playgroud)
警告具体是:
warning: variable 'i' set but not used [-Wunused-but-set-variable]
Run Code Online (Sandbox Code Playgroud)
警告将使意义,如果i
是一个元素的副本,但由于vertexIndices
是一个QList
对象(符合STL-QT容器类)的范围为基础的循环应该调用begin()和end()迭代器干将,这将始终返回一个非const迭代器(只要容器是非const的 - 就是它).
我目前无法测试它是否正常工作,因为我正在改变我的代码库以利用新的C++ 11功能,所以没有任何编译.但是我希望有人可以告诉我这个警告是否是无意义的,或者我是否误解了自动和基于范围的循环...
我试图在函数中推导出迭代器的类型,该函数已经用模板推导出了参数的类型.我想要实现的是替换关键字auto,它在C++ 11标准中具有类似的功能.最初我有以下功能:
template<typename Type> bool activeExtension(Type &arr)
{
for (auto it=arr.begin(),
ite=arr.end();
it!=ite;
++it)
{
if (*it != 0)
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
这适用于C++ 11标准的完美编译.但事情发生了变化,我不能再使用这些功能了.
我正在尝试在没有关键字auto的情况下实现相同的功能.所以我想到了模板.
到目前为止,我尝试的是:
template<typename Type> bool activeExtension(Type &arr)
{
for (Type::iterator it=arr.begin(),
ite=arr.end();
it!=ite;
++it)
{
if (*it != 0)
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
你会怎么解决这个问题?
注意:我通常使用以下类型调用此函数,
template <class T>
struct Generic_t {
typedef std::vector<T> Array;
};
Run Code Online (Sandbox Code Playgroud)
因为我必须实例化具有不同类型的向量.
好吧,所以下面我在c ++中有一个结构:
typedef struct tagRECTEx{
int top;
int bottom;
int left;
int right;
int width=(this->right-this->left);
int height;
} RectEx;
Run Code Online (Sandbox Code Playgroud)
现在我想要做的是当我宣布说RextEx WindowSize时; 我指定WindowSize.left = 250和WindowSize.right = 300,我希望它在WindowSize.width将为我减去这个,所以当我去使用cout <<"Window Width:"WindowSize.width; 它会自动减去右边和左边的50.
我不想这样做:
WindowSize.width=(WindowSize.right-WindowSize.left);
Run Code Online (Sandbox Code Playgroud)
我希望通过以下方式为我服务:
... int width =(this-> right-this-> left); ...
这将是唯一的方法是,如果我这样做:
typedef struct tagRECTEx{
int top;
int bottom;
int left=250;
int right=300;
int width=(this->right-this->left);
int height;
} RectEx;
Run Code Online (Sandbox Code Playgroud)
但我不能这样做,因为他们总会改变.
有人可以帮我解决这个问题,因为我一直试图在谷歌网上找到这个过去5个小时.
谢谢
我有一个看起来像这样的课程:
class Container {
public:
Container(){
Doubles["pi"] = 3.1415;
Doubles["e"] = 2.7182;
Integers["one"] = 1;
Integers["two"] = 2;
}
// Bracket.cpp:23:9: error: 'auto' return without trailing return type
// auto& operator[](const std::string&);
auto& operator[](const std::string& key);
private:
std::map<std::string, double> Doubles;
std::map<std::string, int> Integers;
};
Run Code Online (Sandbox Code Playgroud)
我想重载operator[]
函数以从任何一个Doubles
或Integers
根据传递的键返回一些东西.但是,如果要归还的是a 或a ,我不知道prioi.我想以这种方式实现这个功能:double
int
operator[]
// Compiler error
// Bracket.cpp:30:1: error: 'auto' return without trailing return type
// auto& Container::operator[](const std::string& key){
auto& Container::operator[](const std::string& key){
std::cout …
Run Code Online (Sandbox Code Playgroud) 阅读本文后:在C++中使用auto声明变量是否存在缺点?
我问自己:真的没有一个回答者知道auto
不是一个类型而是存储类说明符.
或者,auto
因为C++ 11与普通C中的存储类说明符不同?
如果是这样,这会破坏C和C++之间的兼容性吗?
(我知道他们的官方从来没有相互支持过,但我的经验是C ++委员会试图尽可能接近C,但是现在可以改变现有的关键字而不是过时的.只是添加一个新的.为什么在这里做这样的突破consistens?)
我对C++比较陌生.我刚刚阅读了关于类型推导的auto关键字.我试过在几个函数中实现它只是为了发现它在处理数学运算符时引起了各种各样的问题.我相信发生的事情是当我实际需要浮点除法(变量'i'和'avg')时,我的函数开始实现整数除法.我使用下面的自动关键字发布了代码.
现在,当我明确地将变量声明为浮点数时,函数运行正常.
这是一个使用auto不首选的例子吗?但是,我肯定会发现它们在生成迭代器时会有所帮助.
namespace Probability
{
/* ExpectedValueDataSet - Calculates the expected value of a data set */
template <typename T, std::size_t N>
double ExpectedValueDataSet(const std::array<T, N>& data)
{
auto i = 0;
auto avg = 0;
for(auto it = data.begin(); it != data.end(); it++)
{
i = it - data.begin() + 1;
avg = ((i-1)/i)*avg + (*it)/i;
}
std::cout << avg << " \n";
return avg;
}
};
Run Code Online (Sandbox Code Playgroud) 代码段说明了几个段落:
#include <boost/hana/fwd/eval_if.hpp>
#include <boost/hana/core/is_a.hpp>
#include <iostream>
#include <functional>
using namespace boost::hana;
template<class arg_t>
decltype(auto) f2(arg_t const& a)
{
constexpr bool b = is_a<std::reference_wrapper<std::string>,
arg_t>;
auto wrapper_case = [&a](auto _) -> std::string&
{ return _(a).get(); };
auto default_case = [&a]() -> arg_t const&
{ return a; };
return eval_if(b, wrapper_case, default_case);
}
int main()
{
int a = 3;
std::string str = "hi!";
auto str_ref = std::ref(str);
std::cout << f2(a) << ", " << f2(str) << ", " << f2(str_ref) …
Run Code Online (Sandbox Code Playgroud) 如果我写这样的代码:
auto n = 2048 * 2048 * 5;
char* buf = new char[n];
Run Code Online (Sandbox Code Playgroud)
那么,auto
从C++ 17中的整数溢出中推导类型是否安全?
我提到Scott Meyers的"更有效的C++"用于自动类型演绎.提到的是与模板类型推导相同的方式,并且提到了3种情况.我的问题属于案例3(当ParamType不是指针或引用时),但结果与所描述的不匹配.
#include <iostream>
int main (void)
{
auto i = 2;
const auto c = &i;
*c = 4;
std::cout << "i is " << i;
}
Run Code Online (Sandbox Code Playgroud)
它应该工作
template<typename T>
void f(const T param);
f(&i); // int *
Run Code Online (Sandbox Code Playgroud)
所以,T
这里应该匹配int *
和完整的类型param
应该是const int *
.
但是,正如上面的程序所示,c
不是,const int *
而是int *
.
有人可以解释我在这里错过了什么吗?
写之间有什么区别:
auto my_var = [expression];
Run Code Online (Sandbox Code Playgroud)
和
auto& my_var = [expression];
Run Code Online (Sandbox Code Playgroud)
A)正在传达什么?
B)是否保证第一个版本是副本?(何时,何时不?)
C)什么时候应该使用第二个“ auto&”?
更新:
一个示例是表达式对引用的求值:
#include <vector>
int main() {
auto ints = std::vector{-1};
auto front_int = ints.front();//front returns a reference, but 'auto' doesn't reflect that (giving us a copy instead)
front_int = 0;
return ints.front();//returns '-1', we didn't update the vector
}
Run Code Online (Sandbox Code Playgroud)
乍一看似乎不直观(但是如果您尝试查看更广阔的前景,这是有道理的)。要“修复”,我们需要使用auto&
版本,但是为什么要这样呢?