I have the following code in C++ and I got the compilation error:
a.cpp: In member function `virtual void Derived<T, D>::run(T&)':
a.cpp:13: error: expected primary-expression before "int"
a.cpp:13: error: expected `;' before "int"
Please help me find out what went wrong here. Thanks a lot.
#include <iostream>
template<typename T> struct Base
{
virtual void run( T& ){}
virtual ~Base(){}
};
template<typename T, typename D> struct Derived : public Base<T>
{
virtual void run( T& t )
{
D d;
d.operator()<int>();//nor does d.operator()<T>(); work
}
};
template<typename T> struct X
{
template<typename R> X(const R& r)
{
std::cout << "X(R)" << std::endl;
ptr = new Derived<T,R>();
}
X():ptr(0)
{
std::cout << "X()" << std::endl;
}
~X()
{
if(ptr)
{
ptr->run(data);
delete ptr;
}
else
{
std::cout << "no ptr" << std::endl;
}
}
Base<T>* ptr;
T data;
};
struct writer
{
template<typename T> void operator()()
{
std::cout << "T "<< std::endl;
}
};
int main()
{
{
writer w;
X<int> xi1((writer()));
}
return 0;
};
In Derived<>::run(), change
d.operator()<int>();
to
d.template operator()<int>();
For further information, see this FAQ:
What is the ->template, .template and ::template syntax about?
Your original version works when compiled with Microsoft C++ Compiler version 15.00.21022.08 that comes with Visual Studio 2008 with the following message:
C:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE\xlocale(342) :
warning C 4530: C++ exception handler used, but unwind semantics are not enabled.
Specify /EHsc
Microsoft (R) Incremental Linker Version 9.00.21022.08 Copyright (C) Microsoft Corporation. All rights reserved.
/out:a.exe a.obj
Related
I am building my program by using the latest Emscripten compiler.
It is based on Clang version 14. Actually it is a small test program which is the following:
#include <iostream>
struct Test {
template<typename T>
static inline void Dump(const T& value) {
std::cout << "[generic] = '" << value << "'\n";
}
template<>
static inline void Dump<std::string>(const std::string& value) {
std::cout << "[std::string] = '" << value << "'\n";
}
};
int main() {
std::string text = "hello";
Test::Dump(text);
return 0;
}
When I build it by Emscripten compiler I got the warning:
D:\em_test>emcc a.cpp
a.cpp:10:24: warning: explicit specialization cannot have a storage class
static inline void Dump<std::string>(const std::string& value) {
~~~~~~~ ^
1 warning generated.
If I just remove static keyword from void Dump<std::string> line
then there will be no warning. However, this code will cause compilation error in Visual Studio:
D:\em_test\a.cpp(17,11): error C2352: 'Test::Dump': illegal call of non-static member function
But this error is expected and clear.
I would like to write a cross-platform program.
So, I think I should simple disable this warning in Emscripten.
However, I can not find any Emscripten (which is based on clang version 14)
command line option for that!
And I am asking advice for that.
Actually I tried to use -Wno-static-inline-explicit-instantiation command line option but it did not help:
D:\em_test>emcc -Wno-static-inline-explicit-instantiation a.cpp
a.cpp:10:24: warning: explicit specialization cannot have a storage class
static inline void Dump<std::string>(const std::string& value) {
~~~~~~~ ^
1 warning generated.
However, I see in Clang version 13 user manual description about -Wstatic-inline-explicit-instantiation option but it is about a slightly another warning text.
Also it seems that Clang version 14 is not fully released, so, there is no public Clang version 14 user manual.
I can not find any Emscripten or Clang command line option to disable the above warning.
Could somebody help me?
Explicit specialization of (both static and non-static) function templates cannot be put into class definitions.
Just put it into the enclosing namespace(i.e somewhere after the class):
#include <iostream>
struct Test {
template <typename T>
static inline void Dump(const T& value) {
std::cout << "[generic] = '" << value << "'\n";
}
};
// Notice Test::
template <>
inline void Test::Dump<std::string>(const std::string& value) {
std::cout << "[std::string] = '" << value << "'\n";
}
int main() {
std::string text = "hello";
Test::Dump(text);
return 0;
}
inline is never necessary for in-class function definitions but it has different meaning for member variables.
inline for out-class is necessary in header files because the explicit specialization is not a template anymore.
I am trying to convert boost::object_pool usage on my old project to new visual studio 2019 project, I am using boost version 1.56
ObjectPool.h
class BOOST_OBJECT_POOL_CHECKER
{
boost::object_pool< T > m_sObjectPool;
template <class Arg1>
T* contruct(Arg1& sArg1)
{
T* temp = m_sObjectPool.construct(sArg1);
return temp;
}
}
MaterialServer.h
class MaterialServer
{
MaterialServer(dword serviceType, std::string path);
Material* NEW_MATERIAL();
}
Material.h
class Material
{
BOOST_OBJECT_POOL_CHECKER<Material> m_poolMATERIAL;
Material(MaterialServer* pMatServer);
}
Material.cpp
Material* MaterialServer::NEW_MATERIAL()
{
//Material* returnMaterial = m_poolMATERIAL.construct(this); << error on vs2019, not correct parameter
Material* returnMaterial = m_poolMATERIAL.construct(*this);
}
got first error
boost_1_56\boost\pool\detail\pool_construct_simple.ipp(19,1): error C2664: 'Material::Material(MaterialServer*)': cannot convert argument 1 from 'const T0' to 'MaterialServer *'
ObjectPool.h(68): message : see reference to function template instantiation 'Material *boost::object_pool<T,boost::default_user_allocator_new_delete>::construct<Arg1>(const T0 &)' being compiled
with
[
T=Material,
Arg1=MaterialServer,
T0=MaterialServer
]
should I need upgrade boost version? because previously this code compiled fine on vs2008, but not compiled on vs2019, this c++11 standard so confusing for me
can I get explanation this behavior?
Frankly, this code cannot have compiled under any compiler.
Note: I'm ignoring numerous typos, omitted semi-colons, omitted template declarators, typedefs and access specifiers to focus on the real issues.
You're passing *this which is Material&. However, the contruct [sic] function takes a MaterialServer*.
So, in fact, the commented line was closer, and makes sense IFF it were a member of MaterialServer, not Material.
It would make a lot more sense, logically, for the material server to "create new materials", anyways, and almost works:
class Material {
public:
Material(MaterialServer* pMatServer);
};
class MaterialServer {
BOOST_OBJECT_POOL_CHECKER<Material> m_poolMATERIAL;
public:
MaterialServer(dword serviceType, std::string path);
Material* NEW_MATERIAL();
};
Material* MaterialServer::NEW_MATERIAL()
{
Material* returnMaterial = m_poolMATERIAL.construct(this);
return returnMaterial;
}
I say /almost/ because construct takes its argument by mutable reference. That won't compile here (this is NOT a mutable lvalue).
So, fixing that:
template <typename Arg1> T* construct(Arg1 sArg1) {
return m_sObjectPool.construct(sArg1);
}
Or, more generically:
template <typename... Arg> T* construct(Arg&&... sArg) {
return m_sObjectPool.construct(std::forward<Arg>(sArg)...);
}
We get "compiling code". We can't link it (the constructors aren't defined).
Adding some more imagined code:
Live On Coliru
#include <boost/pool/object_pool.hpp>
#include <iomanip>
#include <iostream>
#include <string>
#include <atomic>
using dword = uint32_t;
template <typename T> class BOOST_OBJECT_POOL_CHECKER {
boost::object_pool<T> m_sObjectPool;
public:
template <typename... Arg> T* construct(Arg&&... sArg)
{
return m_sObjectPool.construct(std::forward<Arg>(sArg)...);
}
};
class MaterialServer; // forward declare
class Material {
public:
Material(MaterialServer* pMatServer);
};
class MaterialServer {
BOOST_OBJECT_POOL_CHECKER<Material> m_poolMATERIAL;
dword _serviceType;
std::string _path;
public:
MaterialServer(dword serviceType, std::string path)
: _serviceType(serviceType)
, _path(path)
{
}
Material* NEW_MATERIAL();
dword getServiceType() const { return _serviceType; }
std::string_view getPath() const { return _path; }
};
Material* MaterialServer::NEW_MATERIAL()
{
Material* returnMaterial = m_poolMATERIAL.construct(this);
return returnMaterial;
}
Material::Material(MaterialServer* pMatServer)
{
static std::atomic_int id{0};
std::cout << "Material " << id++ << " from server ("
<< pMatServer->getServiceType() << ", "
<< std::quoted(pMatServer->getPath()) << ")\n";
}
int main() {
MaterialServer a(123, "Material/a/resource");
MaterialServer b(234, "Material/b/resource");
a.NEW_MATERIAL();
a.NEW_MATERIAL();
b.NEW_MATERIAL();
a.NEW_MATERIAL();
}
Prints
Material 0 from server (123, "Material/a/resource")
Material 1 from server (123, "Material/a/resource")
Material 2 from server (234, "Material/b/resource")
Material 3 from server (123, "Material/a/resource")
If I have a class with std::atomic_bool or std::mutex member for example, and if I put this class inside std::variant, my g++ will complain with "no matching function for call to std::variant<....>". Now I have to declare my std::mutex member to be static.
g++ (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5) Copyright (C) 2017 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Actual code
#include <iostream>
#include <variant>
#include <mutex>
enum class enFixEngineRunMode {
eFixModeStreet // Fix connection side as initiator/client
,eFixModeStreetStandAlone // Fix connection side as initiator/client
,eFixModeStreetAccpt // Fix connection side as acceptor/server
,eFixModeStreetAccptStandAlone // Fix connection side as acceptor/server
,eFixModeClient // Fix connection side as acceptor/client
,eFixModeClientStandAlone // Fix connection side as acceptor/client
,eFixModeClientInit // Fix connection side as initiator/server
,eFixModeClientInitStandAlone // Fix connection side as initiator/server
,eFixModeInvalid
};
struct FOO {
FOO(int any) { }
void operator()() const {
std::cout << "FOO2" << std::endl;
}
};
template <enum enFixEngineRunMode>
struct BAR {
BAR(double any) { }
void operator()() const {
std::cout << "BAR2" << std::endl;
}
std::mutex m_metux;
};
template<>
struct BAR<enFixEngineRunMode::eFixModeStreetStandAlone> {
BAR(double any) { }
void operator()() const {
std::cout << "eFixModeStreetStandAlone" << std::endl;
}
};
using EngineImpl = std::variant<BAR<enFixEngineRunMode::eFixModeStreet>
, BAR<enFixEngineRunMode::eFixModeStreetStandAlone>
, BAR<enFixEngineRunMode::eFixModeStreetAccpt>
, BAR<enFixEngineRunMode::eFixModeStreetAccptStandAlone>
, BAR<enFixEngineRunMode::eFixModeClient>
, BAR<enFixEngineRunMode::eFixModeClientStandAlone>
, BAR<enFixEngineRunMode::eFixModeClientInit>
, BAR<enFixEngineRunMode::eFixModeClientInitStandAlone>
, BAR<enFixEngineRunMode::eFixModeInvalid>>;
struct Engine {
Engine() : m_engine([&] {
int i = 2;
if (1 == i)
return EngineImpl(BAR<enFixEngineRunMode::eFixModeStreetStandAlone>(0.0));
else return EngineImpl(BAR<enFixEngineRunMode::eFixModeStreet>(0.0));
}()) {}
void operator()() const {
std::visit([](auto const& e){ e(); }, m_engine);
}
EngineImpl m_engine;
};
int main(int argc, const char *argv[], char** env)
{
Engine e;
e();
return 0;
}
compilation error:
variantMain2.cpp:57:70: error: no matching function for call to ‘std::variant<BAR<(enFixEngineRunMode)0>, BAR<(enFixEngineRunMode)1>, BAR<(enFixEngineRunMode)2>, BAR<(enFixEngineRunMode)3>, BAR<(enFixEngineRunMode)4>, BAR<(enFixEngineRunMode)5>, BAR<(enFixEngineRunMode)6>, BAR<(enFixEngineRunMode)7>, BAR<(enFixEngineRunMode)8> >::variant(BAR<(enFixEngineRunMode)0>)’
else return EngineImpl(BAR<enFixEngineRunMode::eFixModeStreet>(0.0));
^
In file included from variantMain2.cpp:2:0:
/opt/rh/devtoolset-7/root/usr/include/c++/7/variant:986:2: note: candidate: template<long unsigned int _Np, class _Up, class ... _Args, class> constexpr std::variant<_Types>::variant(std::in_place_index_t<_Np>, std::initializer_list<_Up>, _Args&& ...)
variant(in_place_index_t<_Np>, initializer_list<_Up> __il,
^~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/variant:986:2: note: template argument deduction/substitution failed:
variantMain2.cpp:57:70: note: ‘BAR<(enFixEngineRunMode)0>’ is not derived from ‘std::in_place_index_t<_Idx>’
else return EngineImpl(BAR<enFixEngineRunMode::eFixModeStreet>(0.0));
^
In file included from variantMain2.cpp:2:0:
/opt/rh/devtoolset-7/root/usr/include/c++/7/variant:977:2: note: candidate: template<long unsigned int _Np, class ... _Args, class> constexpr std::variant<_Types>::variant(std::in_place_index_t<_Np>, _Args&& ...)
variant(in_place_index_t<_Np>, _Args&&... __args)
Because std::variant is not an aggregate, it must move its arguments into its internal storage, and std::mutex is not movable (because doing so would break any concurrent users). Your choices are to make BAR movable (e.g., by storing a std::unique_ptr<std::mutex>), or to avoid the move by using std::in_place_type to construct the object inside the variant.
This code compiles fine on Clang and Visual C++ but not on GCC:
#include <iostream>
template <class T>
struct Test {
Test(T &t) : _t(t) {
}
void method() {
std::cout << _t.Internal::_value << "\n"; // Doesn't work on GCC
std::cout << _t.T::Internal::_value << "\n"; // Work on all compilers
}
private:
T &_t;
};
template <class T>
struct Base {
T _value = 1;
};
template <class T>
struct Child : Base<int> {
using Internal = Base<int>;
int _value = 2;
};
int main(int argc, const char * argv[]) {
Child<float> child;
Test<Child<float>> test(child);
test.method();
return 0;
}
The error message from GCC is
error: 'Internal' has not been declared
9 | std::cout << _t.Internal::_value << "\n";
| ^~~~~~~~
Which one is right ?
Visual C++ and Clang are right in accepting this code.
It was a bug in GCC that prevented it from doing the same. The error as in the question was up to GCC 10, in GCC 11 its wording changed to
error: request for member 'Internal' in non-class type 'T'
9 | std::cout << _t.Internal::_value << "\n";
| ^~~~~~~~
And GCC trunk finally accepts the code as well. Demo: https://gcc.godbolt.org/z/dj34Yhns3
So we could expect the fix in GCC 12.
Not the same as but possibly related to this question about static initializers.
Here the first two functions compile fine, and the last one doesn't in vc++ but does in clang and gcc:
class A {
protected:
std::string protected_member = "yay";
public:
void withNormalBlock();
void withFunctionBlock();
void noLambda();
};
void A::withNormalBlock() {
try {
throw std::exception();
} catch (...) {
[this]() {
std::cout << protected_member << std::endl;
}();
}
}
void A::noLambda() try {
throw std::exception();
} catch (...) {
std::cout << protected_member << std::endl;
}
void A::withFunctionBlock() try {
throw std::exception();
} catch (...) {
[this]() {
// this line is the problem:
std::cout << protected_member << std::endl;
}();
}
in clang (OK)
in gcc (OK)
in vc++ (error C2248: 'A::protected_member' : cannot access protected member declared in class 'A')
vc++ 2015 -- same deal
I can't find anything in the standard to suggest that the handler / catch block of a function-try-block should be exempt from function scope or that the lambda's closure type should change. The code compiles if the access type is changed to all public.
What could the root cause be? Is it a bug, or is it something specific to compiler settings that could be changed?
I'd guess it is a compiler bug. It reports the same error in VS2015 as well. Curiously, an attempt to explicitly simulate the functionality of lambda works without any issues in VS2015
class A
{
protected:
std::string protected_member = "yay";
public:
void withFunctionBlock();
};
void A::withFunctionBlock() try
{
throw std::exception();
}
catch (...)
{
struct Closure {
Closure(A *this_) : this_(this_) {}
void operator ()() const { std::cout << this_->protected_member << std::endl; }
A *this_;
};
Closure(this)();
}
I wonder what VS C++ compiler does differently under the hood...
Seems that this is a bug and the lambda in the catch scope is generated outside class scope. I tried to prove that with typeids but the Visual Studio lambda names are weirdly mangled and the name itself does not prove anything. However, error codes generated by the following snippet show that the names differ:
#include <iostream>
#include <typeinfo>
class Foo {
private:
public:
void testLambda()
try {
auto tryScope = [this]() {};
void (*p)() = tryScope;
}
catch(...)
{
auto catchScope = [this]() {};
void (*p)() = catchScope;
}
};
Error output:
(10): error C2440: 'initializing' : cannot convert from 'Foo::testLambda::<lambda_8a3a8afea7359de4568df0e75ead2a56>' to 'void (__cdecl *)(void)'
(15): error C2440: 'initializing' : cannot convert from '<lambda_8cbc08e7748553fb5ae4e39184491e92>' to 'void (__cdecl *)(void)'