一个例子通常比长期解释更好.
您可以在Coliru上编译并运行此代码段.
(另一个前例子也可用)
#include <map>
#include <iostream>
struct MyClass
{
enum class …Run Code Online (Sandbox Code Playgroud) 如何找出编译器在使用auto关键字时推断出的类型?
示例1:更简单
auto tickTime = 0.001;
Run Code Online (Sandbox Code Playgroud)
这被推断为a float或adouble?
例2:更复杂(以及我目前的头痛):
typedef std::ratio<1, 1> sec;
std::chrono::duration<double, sec > timePerTick2{0.001};
auto nextTickTime = std::chrono::high_resolution_clock::now() + timePerTick2;
Run Code Online (Sandbox Code Playgroud)
什么类型nextTickTime?
我遇到的问题是当我尝试发送nextTickTime时std::cout.我收到以下错误:
./main.cpp: In function ‘int main(int, char**)’:
./main.cpp:143:16: error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’
std::cout << std::setprecision(12) << nextTickTime << std::endl; // time in seconds
^
In file included from /usr/include/c++/4.8.2/iostream:39:0,
from ./main.cpp:10:
/usr/include/c++/4.8.2/ostream:602:5: error: initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) …Run Code Online (Sandbox Code Playgroud) 我正在编写一些用于解析一些文本数据文件的模板类,因此很可能绝大多数的解析错误都是由于数据文件中的错误造成的,这些错误大部分都不是由程序员编写的,因此需要关于为什么应用程序无法加载的好消息,例如:
解析example.txt时出错.[MySectiom] Key的值("notaninteger")不是有效的int
我可以计算出从传递给在类模板函数和成员瓦尔参数文件,段和键名,但我不知道如何让模板函数试图转换为类型的名称.
我当前的代码看起来像,只有普通字符串的特殊化,如下:
template<typename T> T GetValue(const std::wstring §ion, const std::wstring &key)
{
std::map<std::wstring, std::wstring>::iterator it = map[section].find(key);
if(it == map[section].end())
throw ItemDoesNotExist(file, section, key)
else
{
try{return boost::lexical_cast<T>(it->second);}
//needs to get the name from T somehow
catch(...)throw ParseError(file, section, key, it->second, TypeName(T));
}
}
Run Code Online (Sandbox Code Playgroud)
Id而不是必须为数据文件可能使用的每种类型进行特定的重载,因为它们有很多...
此外,我需要一个不会产生任何运行时开销的解决方案,除非发生异常,即完全编译时解决方案是我想要的,因为这个代码被称为吨次并且加载时间已经变得有点长.
编辑:好的,这是我提出的解决方案:
我有一个类型h包含以下内容
#pragma once
template<typename T> const wchar_t *GetTypeName();
#define DEFINE_TYPE_NAME(type, name) \
template<>const wchar_t *GetTypeName<type>(){return name;}
Run Code Online (Sandbox Code Playgroud)
然后我可以在我需要处理的每种类型的cpp文件中使用DEFINE_TYPE_NAME宏(例如,在定义要开始的类型的cpp文件中).
然后链接器能够找到适当的模板特化,只要它在某处定义,否则抛出链接器错误,以便我可以添加类型.
在c ++中使用模板时,我遇到了将typename T转换为string的问题.例如:
template <typename T>
class Matrix {
public:
Matrix() {
//my_type = string type of T. i.e. if T is char. I want my_type to be "char".
}
string my_type;
}
Run Code Online (Sandbox Code Playgroud)
如何将T转换为表示T是什么的字符串.
注意:我只是在玩耍,所以请不要担心什么时候需要这样的东西.
是否有可能获得对象名称?
#include<cstdio>
class one {
public:
int no_of_students;
one() { no_of_students = 0; }
void new_admission() { no_of_students++; }
};
int main() {
one A;
for(int i = 0; i < 99; i++) {
A.new_admission();
}
cout<<"class"<<[classname]<<" "<<[objectname]<<"has "
<<A.no_of_students<<" students";
}
Run Code Online (Sandbox Code Playgroud)
在哪里我可以取名字,像
[classname] = A.classname() = one
[objectname] = A.objectname() = A
Run Code Online (Sandbox Code Playgroud)
C++是否提供了实现此目的的任何机制?
我怎么运行这个main.cpp:
#include <iostream>
#include <typeinfo>
using namespace std;
struct Blah {};
int main() {
cout << typeid(Blah).name() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
通过使用GCC 4.4.4版进行编译:
g++ main.cpp
Run Code Online (Sandbox Code Playgroud)
我明白了:
4Blah
Run Code Online (Sandbox Code Playgroud)
在Visual C++ 2008上,我会得到:
struct Blah
Run Code Online (Sandbox Code Playgroud)
有没有办法让它只是打印Blah或struct Blah?
我想在编译时使用类型的名称.例如,假设我写了:
constexpr size_t my_strlen(const char* s)
{
const char* cp = s;
while(*cp != '\0') { cp++; };
return cp - s;
}
Run Code Online (Sandbox Code Playgroud)
现在我希望:
template <typename T>
constexpr auto type_name_length = my_strlen(typeid(T).name());
Run Code Online (Sandbox Code Playgroud)
但是,唉,typeid(T).name()只是const char*,而不是constexpr ......还有其他一些constexpr方法来获得一个类型的名字吗?
我已经编程了很多年的c/c ++,但今天的偶然发现让我有点好奇......为什么两个输出在下面的代码中产生相同的结果?(arr当然是地址arr[0],即指向arr[0].我本来希望&arr是该指针的地址,但它具有相同的值arr)
int arr[3];
cout << arr << endl;
cout << &arr << endl;
Run Code Online (Sandbox Code Playgroud)
备注:这个问题已经结束,但现在又被打开了.(谢谢 ?)
我知道&arr[0]并arr评估相同的数字,但这不是我的问题!问题是为什么&arr和arr评估相同的数字.如果arr是文字(不存储任何软件),那么编译器应该抱怨并说这arr不是左值.如果地址arr存储在某个地方,那么&arr应该给我该地点的地址.(但这种情况并非如此)
如果我写
const int*arr2 = arr;
那么arr2[i]==arr[i]对于任何整数i,但是&arr2 != arr.
C++ 2011标准库的nextafter和nexttoward函数有什么区别?