过了一会儿,我再次发现了模板模板参数的强大功能.请参阅以下代码段:
template <template <class> class TT, class T>
void foo(TT<T>) {
}
template <class T>
using typer = T;
int main() {
foo<typer>(int{});
}
Run Code Online (Sandbox Code Playgroud)
别名模板作为模板模板参数传递给模板,并进一步用于检测模板的其他参数,因为它是推导的上下文.美女!
然而,当需要推断别名模板本身时,看起来编译器会变得疯狂:
template <template <class> class>
struct tag{};
template <template <class> class TT, class T>
void foo(tag<TT>, TT<T>) {
}
template <class T>
using typer = T;
int main() {
foo(tag<typer>{}, int{});
}
Run Code Online (Sandbox Code Playgroud)
编译器当然是正确的,TT
可以从两者tag<TT>
以及TT<T>
参数中推断出来foo
,并且int{}
模板模板与类型参数模式不匹配.有没有办法保留演绎上下文T
但是在TT
非演绎语境中TT<T> …
我正在为第三方应用程序编写DLL.主要的软件工程师提到应用程序使用__cdecl(/ Gd)调用约定.我需要确保使用它.
另外,第三方提供了一个C++ DLL骨架,它导出如下函数:
#ifdef _EXPORTING
#define DECLSPEC __declspec(dllexport)
#else
#define DECLSPEC __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C" {
#endif
DECLSPEC int ICD_Create(char* id);
....
....
Run Code Online (Sandbox Code Playgroud)
我有点困惑.为什么使用__declspec约定而不是__cdedl导出函数?__declspec支持_cdecl吗?
谢谢.
我有两个函数,其中一个函数作为一个参数,这工作得很好,但我想在我的第二个函数中调用这个传递的函数.
class XY {
public:
void first(void f());
void second();
};
void XY::first(void f()){
}
void XY::second(){
f(); //passed function from first()
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码行:
const char *values[] = { "I", "We", "You", "We"};
std::set<const char*> setValues;
for( int i = 0; i < 3; i++ ) {
const char *val = values[i];
std::set<const char*>::iterator it = setValues.find( val );
if( it == setValues.end() ) {
setValues.insert( val );
}
else {
cout << "Existing value" << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
有了这个,我试图在 a 中插入非重复值set
,但不知何故代码没有点击打印现有元素并且重复值被插入。
这里有什么问题?
如果我传递派生类的对象,则应调用派生类catch块.但输出表明异常是由基类捕获的.为什么?
#include<iostream>
using namespace std;
class Base {};
class Derived: public Base {};
int main()
{
Derived d;
// some other stuff
try {
// Some monitored code
throw d;
}
catch(Base b) {
cout<<"Caught Base Exception";
}
catch(Derived d) { //This catch block is NEVER executed
cout<<"Caught Derived Exception";
}
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我对Linux开发相对较新,已经使用Windows一段时间了.无论如何,我正在Windows和Linux上使用g ++编译C++游戏(需要时使用mingw32),并且正在连接SDL2和SDL2_mixer.在Windows上,只需要将DLL文件放在与可执行文件相同的文件夹中,一切运行正常.但是在Linux上,虽然代码编译得很好,甚至没有一个警告,但我在运行时得到了这个:
./nKaruga: error while loading shared libraries: libSDL2_mixer-2.0.so.0: cannot open shared object file: No such file or directory
Run Code Online (Sandbox Code Playgroud)
虽然说共享库是在同一个文件夹中.我在Stack Overflow上查了几个类似的例子,所有这些都涉及到使用LD_LIBRARY_PATH
,并尝试了但无济于事.
% LD_LIBRARY_PATH=pwd
% export LD_LIBRARY_PATH
% ./nKaruga
./nKaruga: error while loading shared libraries: libSDL2_mixer-2.0.so.0: cannot open shared object file: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我想在不一定具有安装依赖项的管理员权限的系统上分发此程序,因此我将SO放在与可执行文件相同的文件夹中.
谢谢提前!
以下代码无法在gcc 5.3下编译(它是从更大的代码片段中获取的简化版本):
#include <unordered_map>
#include <string>
class Foo {
std::unordered_map<std::string, Foo> m; //"self-referential"
};
int main()
{
Foo f;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
出现以下错误:
g++ --std=c++1y -c rh.cpp
In file included from /usr/local/include/c++/5.3.0/utility:70:0,
from /usr/local/include/c++/5.3.0/unordered_map:38,
from rh.cpp:1:
/usr/local/include/c++/5.3.0/bits/stl_pair.h: In instantiation of ‘struct std::pair<const int, Foo>’:
/usr/local/include/c++/5.3.0/ext/aligned_buffer.h:85:34: required from ‘struct __gnu_cxx::__aligned_buffer<std::pair<const int, Foo> >’
/usr/local/include/c++/5.3.0/bits/hashtable_policy.h:246:43: required from ‘struct std::__detail::_Hash_node_value_base<std::pair<const int, Foo> >’
/usr/local/include/c++/5.3.0/bits/hashtable_policy.h:292:12: required from ‘struct std::__detail::_Hash_node<std::pair<const int, Foo>, false>’
/usr/local/include/c++/5.3.0/bits/hashtable_policy.h:1896:60: required from ‘struct std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const int, Foo>, false> …
Run Code Online (Sandbox Code Playgroud) 我试图特别了解模板和变量模板.考虑一下:
template<int M, int N>
const int gcd1 = gcd1<N, M % N>;
template<int M>
const int gcd1<M, 0> = M;
std::cout << gcd1<9, 6> << "\n";
Run Code Online (Sandbox Code Playgroud)
它打印0
出错了.但是,如果我使用constexpr
而不是const
上面,我会得到正确的答案3
.我再次得到结构模板的正确答案:
template<int M, int N>
struct gcd2 {
static const int value = gcd2<N, M % N>::value;
};
template<int M>
struct gcd2<M, 0> {
static const int value = M;
};
std::cout << gcd2<9, 6>::value << "\n";
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
编辑:
gcd1
编译正常,没有基本案例专业化.怎么会?我正在使用Visual Studio 2015.
定义一个类时,以下是否有效?
T(const T&&) = default;
Run Code Online (Sandbox Code Playgroud)
我在这里阅读有关移动构造函数的内容,它解释了如何隐式声明默认值:
一个类可以有多个移动构造函数,例如,
T::T(const T&&)
和T::T(T&&)
.如果存在一些用户定义的移动构造函数,则用户仍可以使用关键字default强制生成隐式声明的移动构造函数.
在页面底部,它提到了缺陷报告CWG 2171:
CWG 2171 C++ 14
X(const X&&) = default
是非常重要的,是微不足道的.
也许wiki条目只是有一个错误,CWG 2171只是指复制构造函数,而不是移动构造函数?
考虑以下MCVE:
#include <iostream>
int main()
{
void foo(int);
std::cout << foo << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这里,我故意尝试以错误的方式打印指向函数的指针,以便选择接受的操作符<< overloadbool
.
basic_ostream& operator<<( bool value );
Run Code Online (Sandbox Code Playgroud)
令我困惑的是,gcc 7.2和clang 5.0都会产生警告,但编译并链接程序.
同时,Visual Studio 15.5.6没有链接此示例.
就个人而言,我预计,尽管使用的编译器foo
似乎是使用ODR,但此代码根本不会链接.
有人可以解释为什么gcc和clang能够链接该程序吗?